HighTechTalks DotNet Forums  

Caching Binary Output

ASP.net Caching microsoft.public.dotnet.framework.aspnet.caching


Discuss Caching Binary Output in the ASP.net Caching forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Chuck P
 
Posts: n/a

Default Caching Binary Output - 10-30-2007 , 01:07 PM






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.No Cache);

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/dem.../120606-1.aspx
http://aspnet.4guysfromrolla.com/articles/122204-1.aspx

Thanks,


Reply With Quote
  #2  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: Caching Binary Output - 10-30-2007 , 10:30 PM






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.




--------------------
Quote:
From: "Dave Bush" <davembush (AT) dmbcllc (DOT) 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
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 (AT) newsgroup (DOT) 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.No Cache);

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/dem...rticles/120606
-1.aspx
http://aspnet.4guysfromrolla.com/articles/122204-1.aspx

Thanks,




Reply With Quote
  #3  
Old   
Chuck P
 
Posts: n/a

Default Re: Caching Binary Output - 10-31-2007 , 08:58 AM



Dave did you note that I was streaming variable output to the response Not a
URL redirect?

"Dave Bush" wrote:

Quote:
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 (AT) newsgroup (DOT) 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.No Cache);

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/dem.../120606-1.aspx
http://aspnet.4guysfromrolla.com/articles/122204-1.aspx

Thanks,



Reply With Quote
  #4  
Old   
Chuck P
 
Posts: n/a

Default Re: Caching Binary Output - 10-31-2007 , 09:06 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 )?



"Steven Cheng[MSFT]" wrote:

Quote:
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 (AT) dmbcllc (DOT) 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
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 (AT) newsgroup (DOT) 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.No Cache);

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/dem...rticles/120606
-1.aspx
http://aspnet.4guysfromrolla.com/articles/122204-1.aspx

Thanks,





Reply With Quote
  #5  
Old   
Chuck P
 
Posts: n/a

Default Re: Caching Binary Output - 10-31-2007 , 10:41 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.Pu blic)


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.Pu blic)
Response.Cache.SetLastModified(myReader("DateUploa ded"))


"Dave Bush" wrote:

Quote:
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 (AT) newsgroup (DOT) 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
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 (AT) newsgroup (DOT) 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.No Cache);

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/dem.../120606-1.aspx
http://aspnet.4guysfromrolla.com/articles/122204-1.aspx

Thanks,





Reply With Quote
  #6  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: Caching Binary Output - 11-04-2007 , 08:50 PM



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.
--------------------
Quote:
From: =?Utf-8?B?Q2h1Y2sgUA==?= <Chuck (AT) newsgroup (DOT) nospam
References: <9EB87EDD-E222-4135-A764-A4037A6A54DE (AT) microsoft (DOT) com
B24F993B0C5F435EA114AF36874F472C@OfficeVista
Subject: Re: Caching Binary Output
Date: Wed, 31 Oct 2007 07:06:05 -0700

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 )?



"Steven Cheng[MSFT]" wrote:

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.










Reply With Quote
  #7  
Old   
Steven Cheng[MSFT]
 
Posts: n/a

Default Re: Caching Binary Output - 11-07-2007 , 03:49 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.

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

Quote:
Organization: Microsoft
Date: Mon, 05 Nov 2007 02:50:49 GMT
Subject: Re: Caching Binary Output


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 (AT) newsgroup (DOT) nospam
References: <9EB87EDD-E222-4135-A764-A4037A6A54DE (AT) microsoft (DOT) com
B24F993B0C5F435EA114AF36874F472C@OfficeVista
Subject: Re: Caching Binary Output
Date: Wed, 31 Oct 2007 07:06:05 -0700

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 )?



"Steven Cheng[MSFT]" wrote:

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.












Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.