11

I have the following scenario, and I wanted suggestions on what is the best way to handle this. My web app (ASP.NET 2.0 / IIS 6) generates PDF files, and I have a results page with links to those PDFs.

Now, I noticed that if I visit the results page, click on a PDF file (it opens in a new window), then re-generate the PDF file, and click on the same link in the results page, the OLD PDF is shown, instead of the new one. I had to delete the temporary internet files in order to see the new one.

So, since I'm NOT serving an ASPX that actually writes the PDF (and I do not want the Save dialog to show), but straight linking to the PDF file, I want to know what the best way to make sure the user always sees the latest file in the server, not a cached version.

I'm guessing adding no-cache headers is out of the question. But the PDF request would still go through an HTTP handler, so I'd like to know if I should create a specific HTTP handler to intercept requests for PDFs, or if i should do this at the IIS level...however I dont necessarily want to avoid caching ALL PDF's on that site.

Any suggestions? Thanks in advance for the help.

GR7
  • 5,083
  • 8
  • 48
  • 66
  • This is a well known issue with Adobe Acrobat Reader and Internet Explorer. Even if you add the headers, the IE plugin will still cache it.. – Michiel van Vaardegem Nov 22 '11 at 20:55
  • didn't know it was a known issue Michiel, thanks for letting me know. Since you appear to already be familiar with the issue, do you have any links to articles detailing/confirming the issue? Might be useful for people with similar issues for the links to be available in the question. – GR7 Nov 22 '11 at 21:11
  • I solved this problem with a aspx page, which serves my PDF file – Michiel van Vaardegem Nov 25 '11 at 07:17

3 Answers3

14

If your link to the pdf document had a unique querystring appended I believe that would prevent caching. Time in ticks is a good one to use, eg:

string.Format("{0}?t={1}", pdfFileUrl, DateTime.Now.Ticks);
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
  • hey Neil, didn't work. Apparently, adding a random query string would work for ASP/ASPX files, but not for PDF files. I get a 400 bad request response. Any other ideas? – GR7 Nov 23 '11 at 02:34
  • I'm pretty sure it works for anything. In this example http://davidwalsh.name/prevent-cache they are using it with css and javascript - a static resource like a pdf file. Do your generated PDF files exist on disk or are they supplied using a handler? – Neil Thompson Nov 23 '11 at 08:46
  • they exist on disk. let me check the article you suggested and i'll also do a PoC on a simple IIS site requesting a simple PDF, see if it works that way. – GR7 Nov 23 '11 at 17:48
  • 1
    my bad. The reason this was giving me a Bad Request IIS error was because the filename had an ampersand in the filename. Once I tried with a different filename, it worked fine. – GR7 Dec 08 '11 at 03:02
5

I just had a similar issue. I have my page allows users to input data and generate new a pdf file Save clicked. The new pdf file overwrites the old one. In IE8, when user click the pdf link after the Save, the old pdf will always showed (user need to clear the cache to display the new one). After hours of searching, I found that in IIS6, 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'. This is working for me!

jsuen
  • 112
  • 2
  • 8
3

The fact the clearing your temporary internet files gave you the new version shows the browser is the source of the cache. You could turn iis caching off but that wouldn't stop proxies caching the document. If you need to be 100% sure that the user sees that latest version, I suggest using a query string value to cause the url to be different. The query string could be the pdf generation timestamp.

Chris Felstead
  • 1,170
  • 1
  • 9
  • 19