[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Chuck P

10/30/2007 6:07:00 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.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/120...
http://aspnet.4guysfromrolla.com/articles/122...

Thanks,

8 Answers

Dave

10/30/2007 6:29:00 PM

0

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/120...
http://aspnet.4guysfromrolla.com/articles/122...

Thanks,

stcheng

10/31/2007 3:31:00 AM

0

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
>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=/artic...
-1.aspx
>http://aspnet.4guysfromrolla.com/articles/122...
>
>Thanks,
>
>

Chuck P

10/31/2007 1:58:00 PM

0

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@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/120...
> http://aspnet.4guysfromrolla.com/articles/122...
>
> Thanks,
>
>

Chuck P

10/31/2007 2:06:00 PM

0

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.
>
>
>
>
> --------------------
> >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
> >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=/artic...
> -1.aspx
> >http://aspnet.4guysfromrolla.com/articles/122...
> >
> >Thanks,
> >
> >
>
>

Dave

10/31/2007 2:26:00 PM

0

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
> 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/120...
> http://aspnet.4guysfromrolla.com/articles/122...
>
> Thanks,
>
>

Chuck P

10/31/2007 3:41:00 PM

0

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-... 14.25
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetLastModified(myReader("DateUploaded"))


"Dave Bush" wrote:

> 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
> > 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/120...
> > http://aspnet.4guysfromrolla.com/articles/122...
> >
> > Thanks,
> >
> >
>
>

stcheng

11/5/2007 2:51:00 AM

0

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,
>
>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.
>>
>>
>>

>> >
>>
>>
>

stcheng

11/7/2007 9:50:00 AM

0

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,
>
>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,
>>
>>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.
>>>
>>>
>>>
>
>>> >
>>>
>>>
>>
>
>