File download problem with Response.Cache.SetCacheability

T

Tim_Mac

hi,
i read that by adding the following code to by aspx pages, it would not
store temporary internet files:
Response.Cache.SetCacheability(HttpCacheability.NoCache);

it didn't actually work that way, but i left it in anyway. little did
i know it would cause a problem for a forced download routine i have.
instead of downloading the .xls attachment, it tried to download the
aspx file, and gave an error saying the file could not be downloaded,
there could be many reasons for this: path does not exist, access
denied etc.

it took ages to pin down, so i'm just posting this in case anyone
encounters a similar problem.

tim.
 
S

Steven Cheng[MSFT]

Hi Tim_Mac,

Welcome to ASPNET newsgroup.
As for the Response.Cache.SetCacheability(HttpCacheability.NoCache), it
does help to prevent the page from being cached both at serverside or
clientside browser. However, the downloading problem you encountered could
be caused by something else. Would you provide the detailed code of your
file download page? Also, for such problem, we can use some http trace
tools(http trace utliity or webfetch...) to watch the page's response
content and headers to see whether there is anything strange.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Tim_Mac" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: File download problem with Response.Cache.SetCacheability
| Date: 18 Oct 2005 03:54:31 -0700
| Organization: http://groups.google.com
| Lines: 17
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 83.141.121.205
| Mime-Version: 1.0
| Content-Type: text/plain; charset="iso-8859-1"
| X-Trace: posting.google.com 1129632876 18325 127.0.0.1 (18 Oct 2005
10:54:36 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Tue, 18 Oct 2005 10:54:36 +0000 (UTC)
| User-Agent: G2/0.2
| X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.7.10) Gecko/20050716 Firefox/1.0.6,gzip(gfe),gzip(gfe)
| Complaints-To: (e-mail address removed)
| Injection-Info: o13g2000cwo.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!news.glorb.com!postnews.google.com!o13g2000cwo.googlegroups.com!not-fo
r-mail
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:132049
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| hi,
| i read that by adding the following code to by aspx pages, it would not
| store temporary internet files:
| Response.Cache.SetCacheability(HttpCacheability.NoCache);
|
| it didn't actually work that way, but i left it in anyway. little did
| i know it would cause a problem for a forced download routine i have.
| instead of downloading the .xls attachment, it tried to download the
| aspx file, and gave an error saying the file could not be downloaded,
| there could be many reasons for this: path does not exist, access
| denied etc.
|
| it took ages to pin down, so i'm just posting this in case anyone
| encounters a similar problem.
|
| tim.
|
|
 
T

Tim_Mac

hi Steven,
thanks for the reply. yes i suppose you're right it could be the
export code, but when i added the SetCacheability, the export routine
broke, when i took it away, the export worked...

here is the export code, fairly standard i think:

/// <summary>
/// Write a dataset to the HttpResponse as an excel file.
/// </summary>
public static void ExportDataGrid(DataGrid dg, string filename)
{
HttpResponse response = HttpContext.Current.Response;

// clean up the response.object
response.Clear();
response.Charset = "";
response.ContentEncoding = Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment; filename=\"" +
filename + "\"");

// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
dg.RenderControl(htw);
dgText = sw.ToString();
}
}
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();
}

i'll be interested to hear any insight you may have into this
behaviour.
thanks
tim
 
S

Steven Cheng[MSFT]

Thanks for your response Tim,

Yes, the exporting code you provided is standard one and after some further
testing, I think the problem is just caused by the httpheader set by

Response.Cache.SetCacheability(HttpCacheability.NoCache)

I just captured the http messages when setting and not setting the above
"NOCache" option and found that when the http response returned the

Cache-Control: no-cache

header. So we can also reproduce the problem when using the following code:

page_load...
{
Response.CacheControl = "no-cache";
ExportDataGrid(dgSheet,"test.xls");
}

IMO, this should be the clientside browser's behavior against "no-cache"
response with stream content other than the original text/html content. So
would you try avoid setting the CacheAbility or the "Cache-Control" header
to "no-cache" when you'd like to output custom binary file stream?

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Tim_Mac" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: File download problem with Response.Cache.SetCacheability
| Date: 19 Oct 2005 04:10:04 -0700
| Organization: http://groups.google.com
| Lines: 43
| Message-ID: <[email protected]>
| References: <[email protected]>
| <[email protected]>
| NNTP-Posting-Host: 83.141.121.205
| Mime-Version: 1.0
| Content-Type: text/plain; charset="iso-8859-1"
| X-Trace: posting.google.com 1129720209 11894 127.0.0.1 (19 Oct 2005
11:10:09 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Wed, 19 Oct 2005 11:10:09 +0000 (UTC)
| In-Reply-To: <[email protected]>
| User-Agent: G2/0.2
| X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.7.10) Gecko/20050716 Firefox/1.0.6,gzip(gfe),gzip(gfe)
| Complaints-To: (e-mail address removed)
| Injection-Info: g47g2000cwa.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.glorb.com!postnews.google.com!g47g2000cwa.google
groups.com!not-for-mail
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:132397
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| hi Steven,
| thanks for the reply. yes i suppose you're right it could be the
| export code, but when i added the SetCacheability, the export routine
| broke, when i took it away, the export worked...
|
| here is the export code, fairly standard i think:
|
| /// <summary>
| /// Write a dataset to the HttpResponse as an excel file.
| /// </summary>
| public static void ExportDataGrid(DataGrid dg, string filename)
| {
| HttpResponse response = HttpContext.Current.Response;
|
| // clean up the response.object
| response.Clear();
| response.Charset = "";
| response.ContentEncoding = Encoding.UTF8;
| response.Charset = "";
| response.AddHeader("Content-Disposition", "attachment; filename=\"" +
| filename + "\"");
|
| // get the text of the rendered datagrid
| string dgText;
| using (StringWriter sw = new StringWriter())
| {
| using (HtmlTextWriter htw = new HtmlTextWriter(sw))
| {
| // instantiate a datagrid
| dg.RenderControl(htw);
| dgText = sw.ToString();
| }
| }
| response.ContentType = "application/vnd.ms-excel";
| response.Write(dgText);
| response.End();
| }
|
| i'll be interested to hear any insight you may have into this
| behaviour.
| thanks
| tim
|
|
 
T

Tim_Mac

hi Steven,
thanks for the reply. yes i gave up altogether on using
SetCacheability because IE6 still recorded temporary internet files on
the computer. this was the only reason i was using it. i was under
the impression it should prevent temporary internet files. i used the
following 3 meta tags and SetCacheability but still the temp files
appeared.

<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">

that's another story though. thanks anyway for your help.
tim
 
J

Joerg Jooss

Tim_Mac said:
hi Steven,
thanks for the reply. yes i gave up altogether on using
SetCacheability because IE6 still recorded temporary internet files on
the computer. this was the only reason i was using it. i was under
the impression it should prevent temporary internet files. i used the
following 3 meta tags and SetCacheability but still the temp files
appeared.

<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">

First, META tags are pretty useless to control caching, as most proxies
don't care about HTML content.

Second, Cache-Control: no-cache is a misnomer. A local copy may be held
by a proxy or a client, but it *must not* be served without first
validating with the origin server that it's still "fresh". Therefore,
files served with no-cache still show up as local copies.

The correct Cache-Control header to prevent persisting resources is
Cache-Control: no-store, which can be set using
HttpCachePolicy.SetNoStore().

Cheers,
 
S

Steven Cheng[MSFT]

Hi Tim,

Thanks for your followup.
If you really need to ensure that each file download retrieve new content
from server, you can considering always request file download page throw a
button click or other post back operation. Then, we can manually append a
timestamp after the file download page/handler's url. e.g:

http://servername/app/downloadpage.aspx?tid=xxxxxxx

(make sure the xxxxxxx is always updated)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "Tim_Mac" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: File download problem with Response.Cache.SetCacheability
| Date: 22 Oct 2005 11:03:38 -0700
| Organization: http://groups.google.com
| Lines: 15
| Message-ID: <[email protected]>
| References: <[email protected]>
| <[email protected]>
| <[email protected]>
| NNTP-Posting-Host: 83.141.121.205
| Mime-Version: 1.0
| Content-Type: text/plain; charset="iso-8859-1"
| X-Trace: posting.google.com 1130004223 8594 127.0.0.1 (22 Oct 2005
18:03:43 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Sat, 22 Oct 2005 18:03:43 +0000 (UTC)
| In-Reply-To: <[email protected]>
| User-Agent: G2/0.2
| X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.7.10) Gecko/20050716 Firefox/1.0.6,gzip(gfe),gzip(gfe)
| Complaints-To: (e-mail address removed)
| Injection-Info: g14g2000cwa.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.glorb.com!postnews.google.com!g14g2000cwa.google
groups.com!not-for-mail
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:133260
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| hi Steven,
| thanks for the reply. yes i gave up altogether on using
| SetCacheability because IE6 still recorded temporary internet files on
| the computer. this was the only reason i was using it. i was under
| the impression it should prevent temporary internet files. i used the
| following 3 meta tags and SetCacheability but still the temp files
| appeared.
|
| <META Http-Equiv="Cache-Control" Content="no-cache">
| <META Http-Equiv="Pragma" Content="no-cache">
| <META Http-Equiv="Expires" Content="0">
|
| that's another story though. thanks anyway for your help.
| tim
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top