Caching issue when re-generating pdf files

In our recent project, I met an issue regarding PDF cache, our web application will display the PDFs which generated by another web application, however the provide application could re-generate the PDF due to business process, and the consumer web application always wants to get the latest version of the PDF. The reality is the IIS somehow cache the PDF file, so after the first display, no matter the PDF changed on the back end, the consumer web application always show the first launched pdf which is really annoying.

Solution: Basically, there are quite a few methods to resolve this issue. As the root reason is IE cached the PDF, so you can ask the clients to clear the cache, or turn off IIS cache on the server side (go to ‘Output Caching’, add a new cache rule with file extension ‘.aspx’, tick both ‘User-mode caching’ and ‘Kernel-mode caching’ then under both options, select ‘Prevent all caching’).

But all in all, I prefer a lite solution which won’t involve server/client change. The idea is adding a time stamp at the end of url, so IE browser will treat as a new url so that won’t bother cache to display. The only trick is most likely the pdf link is on a hyperlink field or something like that, but the link will generate during the page load phase, so how to change the time stamp at the time when the client click is an issue.


   <asp:GridView ID="mygridview" runat="server" AutoGenerateColumns="False"
        DataKeyNames="id" Width="100%" SkinID="Professional" CssClass="my-gridview"
        GridLines="None" EnableModelValidation="True">
        <AlternatingRowStyle CssClass="aladdin-gridview-altrow" />
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID">
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle ForeColor="#999999" HorizontalAlign="Left" Width="80px" />
            </asp:BoundField>
           
            <asp:BoundField DataField="DisplayDate" DataFormatString="{0:MMMM d, yyyy}" HeaderText=" My Date"    ItemStyle-HorizontalAlign="Center">
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left" Width="300px"></ItemStyle>
            </asp:BoundField>

            <asp:BoundField DataField="DisplayValue" HeaderText="Legislature" ItemStyle-HorizontalAlign="Center">
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
            </asp:BoundField>
      
            <asp:HyperLinkField  DataNavigateUrlFields="id" Target="_blank" 
                DataNavigateUrlFormatString = "mypdf-fromanotherapp{0}.pdf"
                Text="<img title='Print Session' border='0' src='images/sm-pdf.gif' />"   >
                  <ItemStyle Width="80px" HorizontalAlign="Center" VerticalAlign="Top" />
             </asp:HyperLinkField>
   
        </Columns>
        <HeaderStyle CssClass="my-gridview-header" />
        <SelectedRowStyle CssClass="my-gridview-selectedrow" />
    </asp:GridView>

solution a: direct the link to a new page(display in new window/tab) with query parameters, in the page load event, add time stamp and display the PDF in this new page.
In aspx page, should code like below

<a href="<%# "pdfdisplay.aspx?id=" + DataBinder.Eval(Container.DataItem, ("ID")) %>" target="_blank">%></a>

In aspx.cs, should code like below

protected void Page_Load(object sender, EventArgs e)
{
    string strID = Request.QueryString["id"];
    string newurl = String.Format("{0}mypdf_from_anotherAPP{1}.pdf?dt={2}", sitepath,strID,             DateTime.Now.Ticks.ToString()); //This will result in yourFile.pdf?dt=2839238293
    Response.Redirect(strURL);
}

solution b: use javascript to generate a url with timestamp whenever the link being clicked.

Aspx page:

<script type="text/javascript" language="javascript">
         function openPDFPopup(strOpen) {
             var mydt = new Date();
             timestr = mydt.getTime().toString();
             strOpen =strOpen+ '?t=' + timestr;
             var win = window.open(strOpen, '_blank');
             win.focus();
         }
</script>

            <asp:TemplateField >                     
                <ItemTemplate>
                    <a href="javascript:openPDFPopup('<%# String.Format("{0}mypdf-fromanotherapp{1}.pdf",ConfigurationManager.AppSettings("PDFSite"),Eval("id"))%>')">
                    <img src='images/sm-pdf.gif' border=0px/> </a>                                    
                </ItemTemplate>

                <ItemStyle Width="80px" HorizontalAlign="Center" VerticalAlign="Top" />                
            </asp:TemplateField>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s