Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Stream A Pdf File From A Website And Dont Cache It Client Side

- 25 Sep 2008

I had an issue where someone wanted to stream a file that was generated on the web server and prevent it from caching on the client.  The correct way to do this is to specify the Cache-Control: no-cache header.  When this was done however the pdf file did not display using https and on Windows XP.  After some research the method that worked for this scenario was to define an OBJECT tag and point the SRC attribute to the page that generates the file on the fly.

 Example code:

This is an example of the failing page:

<%

<font color=#0000ff size=2>@</font> <font color=#a31515 size=2>Page</font> <font color=#ff0000 size=2>Language</font><font color=#0000ff size=2>=”C#”</font> %> <font color=#0000ff size=2><!</font><font color=#a31515 size=2>DOCTYPE</font> <font color=#ff0000 size=2>html</font> <font color=#ff0000 size=2>PUBLIC</font> <font color=#0000ff size=2>“-//W3C//DTD XHTML 1.0 Transitional//EN”</font> <font color=#0000ff size=2>“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
«/font><font color=#a31515 size=2>script</font> <font color=#ff0000 size=2>runat</font><font color=#0000ff size=2>=”server”>
</font>

</font><font color=#0000ff size=2>protected</font> <font color=#0000ff size=2>void</font> Button1_Click(<font color=#0000ff size=2>object</font> sender, <font color=#2b91af size=2>EventArgs</font> e) {

Response.ContentType = </font><font color=#a31515 size=2>“application/pdf”</font>; Response.Clear();

Response.TransmitFile(

<font color=#a31515 size=2>“leopardsupport.pdf”</font>); Response.End();

}

<font color=#0000ff size=2>

</</font><font color=#a31515 size=2>script</font><font color=#0000ff size=2»
    «/font><font color=#a31515 size=2>html</font> <font color=#ff0000 size=2>xmlns</font><font color=#0000ff size=2>=”http://www.w3.org/1999/xhtml”>
        «/font><font color=#a31515 size=2>head</font> <font color=#ff0000 size=2>runat</font><font color=#0000ff size=2>=”server”>
</font><font color=#0000ff size=2>            «/font><font color=#a31515 size=2>title</font><font color=#0000ff size=2»</font>Untitled Page<font color=#0000ff size=2></</font><font color=#a31515 size=2>title</font><font color=#0000ff size=2»</font>
<font color=#0000ff size=2>        </</font><font color=#a31515 size=2>head</font><font color=#0000ff size=2»
    «/font><font color=#a31515 size=2>body</font><font color=#0000ff size=2»
</font><font color=#0000ff size=2>        «/font><font color=#a31515 size=2>form</font> <font color=#ff0000 size=2>id</font><font color=#0000ff size=2>=”form1″</font> <font color=#ff0000 size=2>runat</font><font color=#0000ff size=2>=”server”>
</font><font color=#0000ff size=2>            «/font><font color=#a31515 size=2>div</font><font color=#0000ff size=2»
</font><font color=#0000ff size=2>            </</font><font color=#a31515 size=2>div</font><font color=#0000ff size=2»
</font><font color=#0000ff size=2>        «/font><font color=#a31515 size=2>asp</font><font color=#0000ff size=2>:</font><font color=#a31515 size=2>Button</font> <font color=#ff0000 size=2>ID</font><font color=#0000ff size=2>=”Button1″</font> <font color=#ff0000 size=2>runat</font><font color=#0000ff size=2>=”server”</font> <font color=#ff0000 size=2>style</font><font color=#0000ff size=2>=”</font><font color=#ff0000 size=2>margin-bottom</font>: <font color=#0000ff size=2>0px”</font> <font color=#ff0000 size=2>              Text</font><font color=#0000ff size=2>=”Button”</font> <font color=#ff0000 size=2>onclick</font><font color=#0000ff size=2>=”Button1_Click”</font> <font color=#0000ff size=2>/>
</font><font color=#0000ff size=2>        </</font><font color=#a31515 size=2>form</font><font color=#0000ff size=2»
    </</font><font color=#a31515 size=2>body</font><font color=#0000ff size=2»
</</font><font color=#a31515 size=2>html</font><font color=#0000ff size=2»
</font> 

This is the solution:

The source page:

<font color=#0000ff size=2><font color=#0000ff size=2>

«/font></font><font color=#a31515 size=2><font color=#a31515 size=2>html</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
</font></font><font color=#0000ff size=2><font color=#0000ff size=2>    «/font></font><font color=#a31515 size=2><font color=#a31515 size=2>head</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
        </font></font><font color=#0000ff size=2><font color=#0000ff size=2>«/font></font><font color=#a31515 size=2><font color=#a31515 size=2>title</font></font><font color=#0000ff size=2><font color=#0000ff size=2»</font></font>PDF File<font color=#0000ff size=2><font color=#0000ff size=2></</font></font><font color=#a31515 size=2><font color=#a31515 size=2>title</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
    </</font></font><font color=#a31515 size=2><font color=#a31515 size=2>head</font></font><font color=#0000ff size=2><font color=#0000ff size=2»

    «/font></font><font color=#a31515 size=2><font color=#a31515 size=2>body</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
        «/font></font><font color=#a31515 size=2><font color=#a31515 size=2>object</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>CLASSID</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=’clsid:CA8A9780-280D-11CF-A24D-444553540000′</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>            TYPE</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=’application/pdf’</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>style</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=’</font></font><font color=#ff0000 size=2><font color=#ff0000 size=2>width</font></font>: <font color=#0000ff size=2><font color=#0000ff size=2>100%</font></font>; <font color=#ff0000 size=2><font color=#ff0000 size=2>
           height</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>100%</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>position</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>absolute</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>top</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>0px</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>left</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>0px</font></font>;             <font color=#ff0000 size=2><font color=#ff0000 size=2>margin</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>0px</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>padding</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>0px</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>border</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>0px</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>right</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>580px</font></font>;<font color=#ff0000 size=2><font color=#ff0000 size=2>bottom</font></font>:<font color=#0000ff size=2><font color=#0000ff size=2>500px</font></font>;<font color=#0000ff size=2><font color=#0000ff size=2>‘>
            </font></font><font color=#0000ff size=2><font color=#0000ff size=2>«/font></font><font color=#a31515 size=2><font color=#a31515 size=2>PARAM</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>NAME</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=’src’</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>VALUE</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=’https://MYSERVER/PDFDnld/DownloadWorker.aspx’>
</font></font><font color=#0000ff size=2><font color=#0000ff size=2>        </</font></font><font color=#a31515 size=2><font color=#a31515 size=2>object</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
</font></font><font color=#0000ff size=2><font color=#0000ff size=2>    </</font></font><font color=#a31515 size=2><font color=#a31515 size=2>body</font></font><font color=#0000ff size=2><font color=#0000ff size=2»
</</font></font><font color=#a31515 size=2><font color=#a31515 size=2>html</font></font><font color=#0000ff size=2><font color=#0000ff size=2»</font></font>

<font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>Here is the page generating the pdf file:</font></font></font>

<%

<font color=#0000ff size=2><font color=#0000ff size=2>@</font></font> <font color=#a31515 size=2><font color=#a31515 size=2>Page</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>Language</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=”C#”</font></font> %> <font color=#0000ff size=2><font color=#0000ff size=2><!</font></font><font color=#a31515 size=2><font color=#a31515 size=2>DOCTYPE</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>html</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>PUBLIC</font></font> <font color=#0000ff size=2><font color=#0000ff size=2>“-//W3C//DTD XHTML 1.0 Transitional//EN”</font></font> <font color=#0000ff size=2><font color=#0000ff size=2>“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
«/font></font><font color=#a31515 size=2><font color=#a31515 size=2>script</font></font> <font color=#ff0000 size=2><font color=#ff0000 size=2>runat</font></font><font color=#0000ff size=2><font color=#0000ff size=2>=”server”>

</font></font>

</font><font color=#0000ff size=2><font color=#0000ff size=2>protected</font></font> <font color=#0000ff size=2><font color=#0000ff size=2>void</font></font> Page_Load(<font color=#0000ff size=2><font color=#0000ff size=2>object</font></font> sender, <font color=#2b91af size=2><font color=#2b91af size=2>EventArgs</font></font> e)

{     Response.ContentType =

<font color=#a31515 size=2><font color=#a31515 size=2>“application/pdf”</font></font>;     Response.Clear();     Response.TransmitFile(<font color=#a31515 size=2><font color=#a31515 size=2>“temp.pdf”</font></font>);     Response.End();

}

</font><font color=#0000ff size=2><font color=#0000ff size=2>

</</font></font><font color=#a31515 size=2><font color=#a31515 size=2>script</font></font><font color=#0000ff size=2><font color=#0000ff size=2»

<font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>You could of course stream a pdf file generated on the fly instead of using TransmitFile.</font></font></font>

<font color=#000000>The reason this works is because the href that is used in the object tag works around the issue per this KB article: http://support.microsoft.com/kb/323308 “Internet Explorer file downloads over SSL do not work with the cache control headers” (see workaround section).  This would apply for the no-store header as well.</font>

<font color=#000000>Let me know if this BLOG entry helped you!</font>

<font color=#000000></font> 

</font></font>

<< Go Back