提供文件时是否应该设置 Cache-Control 标头?或不?
我通过 asp.net 中的 HTTPModule 提供一些文件。我想知道设置或不设置 Cache-Control 标头(例如无缓存)是否有任何好处?
编辑:我对此感到好奇的原因是我们遇到了一个问题,即在 IE 中通过 SSL 会话提供 Office 文档会导致错误(缓存控制设置为无缓存) 。也就是说,如果您将 Cache-Control 设置为 no-cache,则无法在 IE 中通过 SSL 下载 Office 文档。
基本上我不想包含 Cache-Control 标头,但想知道它是否会导致问题?
编辑2:好了,Cache-Control 标头已经出来了。我尝试了下面的建议,但遇到了一些问题。每当我添加过期标头或更改缓存控制时,当我尝试打开 Office 2007 文档时,它会尝试将其作为 zip 文件打开。 (我知道它们实际上是隐藏的 zip 文件)但是当我不使用过期标头或缓存控制时,IE 可以像 Office 文档一样打开它们。不幸的是,我没有时间尝试解决所有这些问题 - 因为从现在起十分钟后代码就会冻结:)
感谢大家的帮助!
I'm serving up some files via an HTTPModule in asp.net. I want to know if there are any benefits to setting, or not setting, the Cache-Control header to something (like no-cache)?
Edit: The reason I'm curious about this is because we ran in to a problem where serving up office documents over an SSL session in IE results in an error (with Cache Control set to no-cache). That is to say, you cannot download office docs over SSL in IE if you have set Cache-Control to no-cache.
Basically I want to NOT include the Cache-Control header, but wonder if it will cause problems?
Edit 2: Well, the Cache-Control header is out. I tried the suggestions below but had some problems. Any time I add an expires header, or change Cache-Control at all, when I try and open an Office 2007 document it tries to open it as a zip. (I know that they're really zip files under the covers) but when I don't use an expires header or cache-control IE opens them just fine as Office Documents. Unfortunately I don't have time to try and figure all this out - as code freeze is ten minutes from now :)
Thanks everyone for trying to help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据雅虎!和 YSlow 你应该。请参阅这篇文章。
更新:根据您的评论,您似乎正在尝试阻止缓存。我会使用:
对我来说,它比使用 Expires 标头更简单、更明确。
更新 2:听起来您需要为 Office 文档指定 Content-Type 标头。尝试使用以下:
According to Yahoo! and YSlow you should. See this article.
Update: Based on your comment it looks like you are trying to prevent caching. I would use:
To me, it's simpler and more explicit than using an Expires header.
Update 2: Sounds like you need to specify the Content-Type header for the office documents. Try using below:
您可以尝试将
Expires
标头设置为过去的日期/时间,而不是使用Cache-Control
。Instead of using
Cache-Control
you could try to set theExpires
header to a past date/time.这绝对是一个奇怪的问题...但这里有一个快速解决方案,可以防止您的用户获得过时的内容:
如果您在 URL 末尾附加一个额外的查询字符串参数以使对 Office 文件的每个请求都是唯一的,您可以获得无需在标头中设置缓存控制信息。
您当前的 URL 可能如下所示:
http://mysite.com/filegetter?name=document。 doc
使用附加的“唯一”参数:
http://mysite.doc com/filegetter?name=document.doc&ts=
这将防止浏览器向用户提供过时的 Office 文件,并且该方法可以在客户端或服务器代码中实现。处理将文件传送回用户的模块只是忽略 URL 中使其对于用户浏览器而言唯一的部分。
That is definitely an odd issue... but here is quick solution that could prevent your users from getting stale content:
If you append an additional querystring parameter to the end of your URL to make each request for the Office file unique, you can get away without setting the cache-control information in the header.
Your current URL may look like this:
http://mysite.com/filegetter?name=document.doc
With the additional "unique" parameter:
http://mysite.com/filegetter?name=document.doc&ts=
This will prevent the browser from giving your user a stale office file, and the method could be implemented in client or server code. The module that handles shipping the file back to your users simply ignores the portion of the URL that makes it unique to your user's browser.
我必须使用“no-store”在没有缓存的情况下在 IE6 中成功渲染 PDF 文件。
I had to use "no-store" to successfully render PDF files inside IE6 with no caching.