Groups | Blog | Home
all groups > asp.net caching > october 2007 >

asp.net caching : Caching Binary Output


Chuck P
10/30/2007 11:07:01 AM
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
10/30/2007 2:28:47 PM
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,
stcheng NO[at]SPAM online.microsoft.com
10/31/2007 12:00:00 AM
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.




--------------------
[quoted text, click to view]
Chuck P
10/31/2007 6:58:03 AM
Dave did you note that I was streaming variable output to the response Not a
URL redirect?

[quoted text, click to view]
Chuck P
10/31/2007 7:06:05 AM
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 )?



[quoted text, click to view]
Chuck P
10/31/2007 8:41:00 AM
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"))


[quoted text, click to view]
Dave Bush
10/31/2007 10:26:11 AM
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?

[quoted text, click to view]
stcheng@online.microsoft.com
11/5/2007 2:50:49 AM
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.
--------------------
[quoted text, click to view]
stcheng@online.microsoft.com
11/7/2007 9:49:37 AM
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.

--------------------

[quoted text, click to view]
AddThis Social Bookmark Button