使ASP.NET / IIS不能缓存PDF文件的正确方法

时间:2021-05-19 03:49:27

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.

我有以下场景,我想知道什么是处理这个的最佳方法。我的Web应用程序(ASP.NET 2.0 / IIS 6)生成PDF文件,我有一个结果页面,其中包含指向这些PDF的链接。

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.

现在,我注意到如果我访问结果页面,单击PDF文件(它将在新窗口中打开),然后重新生成PDF文件,然后单击结果页面中的相同链接,将显示OLD PDF而不是新的。我不得不删除临时的互联网文件,以便查看新的。

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.

所以,既然我没有提供实际编写PDF的ASPX(我不希望显示“保存”对话框),而是直接链接到PDF文件,我想知道确保用户始终确保用户的最佳方法看到服务器中的最新文件,而不是缓存版本。

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.

我猜测添加无缓存标头是不可能的。但PDF请求仍然会通过HTTP处理程序,所以我想知道我是否应该创建一个特定的HTTP处理程序来拦截PDF的请求,或者我是否应该在IIS级别执行此操作...但是我不一定希望避免在该网站上缓存所有PDF文件。

Any suggestions? Thanks in advance for the help.

有什么建议么?先谢谢您的帮助。

3 个解决方案

#1


12  

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:

如果您的pdf文档链接附加了一个唯一的查询字符串,我相信这会阻止缓存。时间刻度是一个很好的使用,例如:

string.Format("{0}?t={1}", pdfFileUrl, DateTime.Now.Ticks);

#2


4  

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!

我刚才有类似的问题。我有我的页面允许用户输入数据并生成新的pdf文件保存点击。新的pdf文件会覆盖旧的pdf文件。在IE8中,当用户在Save之后单击pdf链接时,将始终显示旧的pdf(用户需要清除缓存以显示新缓存)。经过几个小时的搜索,我发现在IIS6中,转到“输出缓存”,添加一个文件扩展名为“.aspx”的新缓存规则,勾选“用户模式缓存”和“内核模式缓存”,然后在两个选项下,选择“防止所有缓存”。这对我有用!

#3


2  

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.

清除临时Internet文件为您提供新版本的事实表明浏览器是缓存的来源。您可以关闭iis缓存,但这不会阻止代理缓存文档。如果您需要100%确定用户看到最新版本,我建议使用查询字符串值来使网址不同。查询字符串可以是pdf生成时间戳。

#1


12  

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:

如果您的pdf文档链接附加了一个唯一的查询字符串,我相信这会阻止缓存。时间刻度是一个很好的使用,例如:

string.Format("{0}?t={1}", pdfFileUrl, DateTime.Now.Ticks);

#2


4  

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!

我刚才有类似的问题。我有我的页面允许用户输入数据并生成新的pdf文件保存点击。新的pdf文件会覆盖旧的pdf文件。在IE8中,当用户在Save之后单击pdf链接时,将始终显示旧的pdf(用户需要清除缓存以显示新缓存)。经过几个小时的搜索,我发现在IIS6中,转到“输出缓存”,添加一个文件扩展名为“.aspx”的新缓存规则,勾选“用户模式缓存”和“内核模式缓存”,然后在两个选项下,选择“防止所有缓存”。这对我有用!

#3


2  

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.

清除临时Internet文件为您提供新版本的事实表明浏览器是缓存的来源。您可以关闭iis缓存,但这不会阻止代理缓存文档。如果您需要100%确定用户看到最新版本,我建议使用查询字符串值来使网址不同。查询字符串可以是pdf生成时间戳。