在 IE 中启用 pdf 缓存

发布于 2024-08-13 06:09:34 字数 602 浏览 6 评论 0 原文

起初,缓存并非在所有浏览器中都起作用。然后我通过在 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,我使用 Adob​​e Reader 9.2.0。

如何让它在 IE 中也能工作?我注意到 IE 向请求添加了“Cache-Control: no-cache”标头,而 Safari 则没有。

At first, caching didn't work in all browsers. Then I made it work in all browsers but IE (IE8) by adding .pdf extension to the url. Servlet stopped being called after that.

I display pdf file inline on the webpage via EMBED tag that loads the following url:

http://localhost:7001/app/viewFile.pdf

Which is generated by java servlet with the following headers:

response.addHeader("Content-Disposition", "inline;");
response.setHeader("Cache-control", "cache,max-age=600");
response.setContentType(mimeType);
response.setContentLength(contentLength);

For pdf displaying in all browsers I use Adobe Reader 9.2.0.

How to make it work in IE too? I noticed that IE adds 'Cache-Control: no-cache' header to request, whereas Safari, for example, doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋心╮凉 2024-08-20 06:09:34

如前所述,缓存控制 标头值 cache 无效。请改用 public

至于IE在embedobject元素中不遵守服务器端缓存控制规则,不幸的是,这是IE的一个“特性”。您能做的最好的事情就是用 iframe 元素替换它。

其他标头如 expireslast-modifiedetag 等不会有帮助。

As said before, the cache-control header value cache is invalid. Use public instead.

As to IE not adhering the server-side cache control rules in the embed and object elements, this is unfortunately a "feature" of IE. Best what you can do is replacing it by an iframe element.

Other headers like expires, last-modified, etag and so on ain't going to help.

錯遇了你 2024-08-20 06:09:34

需要研究的一些想法:

  1. 我认为 cache 不是有效的 Cache-Control 指令。

    尝试使用 public 值,或者使用 private 值(如果这更适合您的内容)。查看 RFC 2616 了解更多信息。

  2. 也许您发送了不止一封
    Cache-Control指令?

    使用工具
    例如 Firebug
    LiveHTTPHeaders 查看
    您的浏览器的实际标头
    接收。确保他们不是
    得到一些你意想不到的东西。
    听起来你可能已经是
    虽然这样做。另请确保您没有同时发送 Pragma: no-cache

  3. 尝试设置
    除了使用 Expires 标头之外
    缓存控制

    这也是可以的
    您正在发送浏览器
    冲突的Cache-Control/Pragma
    标头和 IE 选择采用
    无论出于何种原因,Pragma 标头作为第一优先级。

  4. 确保 IE 配置为允许缓存! :)

    控制面板 >
    Internet 选项 >
    临时 Internet 文件 >
    设置>
    检查存储页面的较新版本

  5. 尝试发送 PDF 作为对 POST 请求的响应(通过表单提交)。

    由于 RFC 2616:“默认情况下,如果请求方法、请求标头字段、响应状态的要求表明响应是可缓存的,则响应是可缓存的。”对 POST 请求的响应不可缓存,因此 IE 不应在其请求中包含该标头。

  6. 尝试发送具有一致值的 Content-MD5Last-Modified 标头(如果尚未发送)。

    这可能有助于让 IE 确信 PDF 的内容没有更改。我认为这个解决方案行不通,因为 IE 显然很顽固,但值得一提。

Some ideas to look into:

  1. I don't think cache is a valid Cache-Control directive.

    Try using a value of public instead, or private if that's more appropriate to your content. Check out RFC 2616 for more information.

  2. 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.

  3. Try setting the
    Expires header in addition to using
    Cache-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.

  4. Make sure IE is configured to allow caching! :)

    Control Panel >
    Internet Options >
    Temporary Internet Files >
    Settings >
    Check for newer versions of stored pages

  5. 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.

  6. Try sending the Content-MD5 and Last-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.

辞别 2024-08-20 06:09:34

解决该问题的一种明显方法是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文