在 IE 中启用 pdf 缓存
起初,缓存并非在所有浏览器中都起作用。然后我通过在 url 中添加 .pdf 扩展名使其在除 IE (IE8) 之外的所有浏览器中都能工作。此后 Servlet 不再被调用。
我通过加载以下 url 的 EMBED 标签在网页上内联显示 pdf 文件:
http://localhost:7001/app/viewFile.pdf
由具有以下标题的 java servlet 生成:
response.addHeader("Content-Disposition", "inline;");
response.setHeader("Cache-control", "cache,max-age=600");
response.setContentType(mimeType);
response.setContentLength(contentLength);
对于在所有浏览器中显示的 pdf,我使用 Adobe Reader 9.2.0。
如何让它在 IE 中也能工作?我注意到 IE 向请求添加了“Cache-Control: no-cache”标头,而 Safari 则没有。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如前所述,
缓存控制
标头值cache
无效。请改用public
。至于IE在
embed
和object
元素中不遵守服务器端缓存控制规则,不幸的是,这是IE的一个“特性”。您能做的最好的事情就是用iframe
元素替换它。其他标头如
expires
、last-modified
、etag
等不会有帮助。As said before, the
cache-control
header valuecache
is invalid. Usepublic
instead.As to IE not adhering the server-side cache control rules in the
embed
andobject
elements, this is unfortunately a "feature" of IE. Best what you can do is replacing it by aniframe
element.Other headers like
expires
,last-modified
,etag
and so on ain't going to help.需要研究的一些想法:
我认为
cache
不是有效的Cache-Control
指令。尝试使用
public
值,或者使用private
值(如果这更适合您的内容)。查看 RFC 2616 了解更多信息。也许您发送了不止一封
Cache-Control
指令?使用工具
例如 Firebug 或
LiveHTTPHeaders 查看
您的浏览器的实际标头
接收。确保他们不是
得到一些你意想不到的东西。
听起来你可能已经是
虽然这样做。另请确保您没有同时发送
Pragma: no-cache
。尝试设置
除了使用
Expires
标头之外缓存控制
。这也是可以的
您正在发送浏览器
冲突的
Cache-Control
/Pragma
标头和 IE 选择采用
无论出于何种原因,
Pragma
标头作为第一优先级。确保 IE 配置为允许缓存! :)
控制面板
>Internet 选项
>临时 Internet 文件
>设置
>检查存储页面的较新版本
尝试发送 PDF 作为对
POST
请求的响应(通过表单提交)。由于 RFC 2616:“默认情况下,如果请求方法、请求标头字段、和响应状态的要求表明响应是可缓存的,则响应是可缓存的。”对
POST
请求的响应不可缓存,因此 IE 不应在其请求中包含该标头。尝试发送具有一致值的
Content-MD5
和Last-Modified
标头(如果尚未发送)。这可能有助于让 IE 确信 PDF 的内容没有更改。我认为这个解决方案行不通,因为 IE 显然很顽固,但值得一提。
Some ideas to look into:
I don't think
cache
is a validCache-Control
directive.Try using a value of
public
instead, orprivate
if that's more appropriate to your content. Check out RFC 2616 for more information.Perhaps you're sending more than one
Cache-Control
directive?Use a tool
like Firebug or
LiveHTTPHeaders to peek at the
actual headers your browsers are
receiving. Make sure they're not
getting something you don't expect.
It sounds like you might already be
doing this though. Also make sure you're not also sending
Pragma: no-cache
.Try setting the
Expires
header in addition to usingCache-Control
.It's also possible
you're sending the browser
conflicting
Cache-Control
/Pragma
headers and IE chooses to take the
Pragma
headers as first priority for whatever backwards reason.Make sure IE is configured to allow caching! :)
Control Panel
>Internet Options
>Temporary Internet Files
>Settings
>Check for newer versions of stored pages
Try sending the PDF as a response to a
POST
request (via form submit).IE is allowing a cache to take place regardless of the headers in the response due to this requirement from RFC 2616: "By default, a response is cacheable if the requirements of the request method, request header fields, and the response status indicate that it is cacheable." Responses to
POST
requests are NOT cacheable, so IE shouldn't include that header in it's request.Try sending the
Content-MD5
andLast-Modified
headers with consistent values (if they're not already being sent).This might help convince IE that the content of the PDF has not changed. I don't think this solution will work, because IE is obviously stubborn, but it's worth mentioning.
解决该问题的一种明显方法是使用 URL 重写。如果 IE 在扩展名中使用 .pdf,请使用 mod_rewrite (Apache) 或类似工具将服务器端重定向到正确的页面,同时使客户端认为它确实在请求 PDF 文件。
另外:使用 Fiddler 等工具查看客户端正在接收的 HTTP 标头。
另外:回顾这个旧问题(PHP:强制文件下载和 IE,但是再次),我也遇到过类似的问题,IE 不强制下载。
Well, one obvious way around the problem is to use URL rewriting. If IE works with .pdf in the extension, use mod_rewrite (Apache) or a similar tool to server-side redirect to the right page, while making the client think that it's indeed requesting a PDF file.
Also: review the HTTP headers that the client is receiving using a tool like Fiddler.
Also: review this older question (PHP: Force file download and IE, yet again), I've had similar problems with IE not forcing downloads, too.