By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

File download problem with Response.Cache.SetCacheability

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

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.

Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Tim_Mac,

Welcome to ASPNET newsgroup.
As for the Response.Cache.SetCacheability(HttpCacheability.No Cache), 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" <ti*@mackey.ie>
| 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: <11*********************@o13g2000cwo.googlegroups. com>
| 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: gr**********@google.com
| 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: gr**********@google.com
| Injection-Info: o13g2000cwo.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfee d00.sul.t-online.de!t-onli
ne.de!news.glorb.com!postnews.google.com!o13g2000c wo.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.No Cache);
|
| 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.
|
|

Nov 19 '05 #2

P: n/a
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

Nov 19 '05 #3

P: n/a
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.No Cache)

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" <ti*@mackey.ie>
| 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: <11*********************@g47g2000cwa.googlegroups. com>
| References: <11*********************@o13g2000cwo.googlegroups. com>
| <N$**************@TK2MSFTNGXA01.phx.gbl>
| 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: gr**********@google.com
| NNTP-Posting-Date: Wed, 19 Oct 2005 11:10:09 +0000 (UTC)
| In-Reply-To: <N$**************@TK2MSFTNGXA01.phx.gbl>
| 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: gr**********@google.com
| Injection-Info: g47g2000cwa.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornad o.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.glorb.com!postnews.goo gle.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
|
|

Nov 19 '05 #4

P: n/a
Tim_Mac wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #5

P: n/a
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" <ti*@mackey.ie>
| 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: <11**********************@g14g2000cwa.googlegroups .com>
| References: <11*********************@o13g2000cwo.googlegroups. com>
| <11*********************@g47g2000cwa.googlegroups. com>
| <O7*************@TK2MSFTNGXA01.phx.gbl>
| 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: gr**********@google.com
| NNTP-Posting-Date: Sat, 22 Oct 2005 18:03:43 +0000 (UTC)
| In-Reply-To: <O7*************@TK2MSFTNGXA01.phx.gbl>
| 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: gr**********@google.com
| Injection-Info: g14g2000cwa.googlegroups.com; posting-host=83.141.121.205;
| posting-account=UaxKfw0AAAA4oMLJHydK195yIv1avAma
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornad o.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.glorb.com!postnews.goo gle.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
|
|

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.