.NET Forum / ASP.NET / Caching / November 2007
Caching Binary Output
|
|
Thread rating:  |
Chuck P - 30 Oct 2007 19:07 GMT I have a web page with a Button on it that when clicked displays files (doc, pdf, etc. ) that are stored in a database.
Is their a way to take advantage of caching? I was wondering if an If-Modified-Since header could be used?
Also are any of the caching statements necessary in my method? There are duplicate fileNames in the database!
When the button is clicked the following method is executed.
public static void DisplayFile(byte[] fileData, string fileName, string contentType) {
HttpResponse response = HttpContext.Current.Response;
response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); response.Cache.SetNoStore(); response.Cache.SetCacheability(HttpCacheability.NoCache);
response.Buffer = true; response.Clear();
response.AppendHeader("Content-Disposition", "attachment;filename= \"" + fileName + "\"");
response.AppendHeader("Content-Length", fileData.Length.ToString()); response.ContentType = contentType;
response.OutputStream.Write(fileData, 0, fileData.Length);
response.OutputStream.Flush(); response.OutputStream.Close();
response.Flush(); response.Close();
}
These references got me wondering.
http://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/120606-1.aspx http://aspnet.4guysfromrolla.com/articles/122204-1.aspx
Thanks,
Dave Bush - 30 Oct 2007 19:28 GMT Sure, I do it all the time. The easiest way is to create an aspx page and put the caching directives in the aspx file and in the page_load event of your "code behind" put a variation of the code you have below.
You will, of course, need to Clear the output stream, change the mime type, and do a Request.End() at the end of the page_load, but it definitely works.
-----Original Message----- From: Chuck P [mailto:Chuck@newsgroup.nospam] Posted At: Tuesday, October 30, 2007 2:07 PM Posted To: microsoft.public.dotnet.framework.aspnet.caching Conversation: Caching Binary Output Subject: Caching Binary Output
I have a web page with a Button on it that when clicked displays files (doc, pdf, etc. ) that are stored in a database.
Is their a way to take advantage of caching? I was wondering if an If-Modified-Since header could be used?
Also are any of the caching statements necessary in my method? There are duplicate fileNames in the database!
When the button is clicked the following method is executed.
public static void DisplayFile(byte[] fileData, string fileName, string contentType) {
HttpResponse response = HttpContext.Current.Response;
response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); response.Cache.SetNoStore(); response.Cache.SetCacheability(HttpCacheability.NoCache);
response.Buffer = true; response.Clear();
response.AppendHeader("Content-Disposition", "attachment;filename= \"" + fileName + "\"");
response.AppendHeader("Content-Length", fileData.Length.ToString()); response.ContentType = contentType;
response.OutputStream.Write(fileData, 0, fileData.Length);
response.OutputStream.Flush(); response.OutputStream.Close();
response.Flush(); response.Close();
}
These references got me wondering.
http://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/120606-1.aspx http://aspnet.4guysfromrolla.com/articles/122204-1.aspx
Thanks,
Steven Cheng[MSFT] - 31 Oct 2007 04:30 GMT Thanks for Dave's input.
Hi Chuck,
The one you mentioned is client cache which is a feature of the http 1.1 protocol. The ASP.NET Cache API(which set client cache ) actually use the "Cache-Control" http header to supply the http cache information so as to let the client browser know how to cache the response content. AS in the MSDN document said, you can look up the RFCC 2616- HTTP/1.1 spec and find the detailed description on "cache-control" in section 14.9
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Dave Bush" <davembush@dmbcllc.com> >Subject: Re: Caching Binary Output >Date: Tue, 30 Oct 2007 14:28:47 -0400
>Sure, I do it all the time. The easiest way is to create an aspx page and >put the caching directives in the aspx file and in the page_load event of [quoted text clipped - 59 lines] > >Thanks, Chuck P - 31 Oct 2007 15:06 GMT Steven,
I wasn't sure from section 14.9 how the browser identifies the request for caching or if the caching works the same for various content types. Does it use the URL and any parameters but not form fields?
If it uses the URL and parameters, I could change my code to not stream from the original URL but redirect to somePage.aspx?UniqueContentID=1
Which would probably get cached then (content type aapplication/x-msdownload, application/pdf )?
> Thanks for Dave's input. > [quoted text clipped - 84 lines] > > > >Thanks, Steven Cheng[MSFT] - 05 Nov 2007 02:50 GMT Thanks for your reply Chuck,
Based on my understanding, the http protocol's cache is specified through some header variables in request or response header collection. Sure, url (include querystring) will influence how the client browser cache the request. Normally, a cache will be generated for a fixed url, if anything in the url changed, it will retrieve response from server.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights. --------------------
>From: =?Utf-8?B?Q2h1Y2sgUA==?= <Chuck@newsgroup.nospam> >References: <9EB87EDD-E222-4135-A764-A4037A6A54DE@microsoft.com> <B24F993B0C5F435EA114AF36874F472C@OfficeVista>
>Subject: Re: Caching Binary Output >Date: Wed, 31 Oct 2007 07:06:05 -0700
>Steven, > [quoted text clipped - 26 lines] >> >> This posting is provided "AS IS" with no warranties, and confers no rights. Steven Cheng[MSFT] - 07 Nov 2007 09:49 GMT Hi Chuck,
Any further progress on this? Please feel free to post here if there is anything we can help.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Organization: Microsoft >Date: Mon, 05 Nov 2007 02:50:49 GMT >Subject: Re: Caching Binary Output
>Thanks for your reply Chuck, > [quoted text clipped - 55 lines] >>> This posting is provided "AS IS" with no warranties, and confers no >rights. Chuck P - 31 Oct 2007 14:58 GMT Dave did you note that I was streaming variable output to the response Not a URL redirect?
> Sure, I do it all the time. The easiest way is to create an aspx page and > put the caching directives in the aspx file and in the page_load event of [quoted text clipped - 59 lines] > > Thanks, Dave Bush - 31 Oct 2007 15:26 GMT Yes, and so do I. Here's a sample from returning a flash file from a database.
private void Page_Load(object sender, System.EventArgs e) { Response.ClearContent(); Response.ClearHeaders(); StoreProductController spc = new StoreProductController(); DataSetStoreProduct.dmbcllcStoreProductGetRow info = spc.Get(Convert.ToInt32(Request.QueryString["id"]));
Response.ContentType = "application/x-shockwave-flash"; foreach(byte b in info.Flash) Response.OutputStream.WriteByte(b); Response.End(); }
The Response object you are writing to is EXACTLY the same Response object I write to.
-----Original Message----- From: Chuck P [mailto:Chuck@newsgroup.nospam] Posted At: Wednesday, October 31, 2007 9:58 AM Posted To: microsoft.public.dotnet.framework.aspnet.caching Conversation: Caching Binary Output Subject: Re: Caching Binary Output
Dave did you note that I was streaming variable output to the response Not a URL redirect?
"Dave Bush" wrote:
> Sure, I do it all the time. The easiest way is to create an aspx page > and [quoted text clipped - 61 lines] > > Thanks, Chuck P - 31 Oct 2007 16:41 GMT Dave,
I think the difference is that you have a Request.QueryString["id"] in the URL. My page does not. So your page could probably be reliably cached on the browser. Response.Cache.SetCacheability(HttpCacheability.Public)
I was looking at using the If-Modified-Since request header so that the browser would not use the cache if the very large file in the database had changed since the last request. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 14.25 Response.Cache.SetCacheability(HttpCacheability.Public) Response.Cache.SetLastModified(myReader("DateUploaded"))
> Yes, and so do I. > Here's a sample from returning a flash file from a database. [quoted text clipped - 94 lines] > > > > Thanks,
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|