IE 8和客户端缓存

7

背景故事:

我在一个运行.NET 3.5的IIS 6网络服务器上拥有一个Web门户。目前有一个页面,该页面被赋予一个值,并基于该值在Web服务上查找PDF文件并在Web页面的另一个选项卡中向用户显示结果。这是使用下面的代码完成的。

 context.Response.ClearContent();
 context.Response.ClearHeaders();
 context.Response.Clear();
 context.Response.AddHeader("Accept-Header", pdfStream.Length.ToString());                                               
 context.Response.ContentType = "application/pdf";
 context.Response.BinaryWrite(pdfStream.ToArray());
 context.Response.Flush();

这个方案已经实施多年,并且一直有效。但是,客户报告说特定客户在清除了临时互联网缓存之前每次返回相同的PDF文件。
我认为这很简单,只需添加缓存标头到响应中以永远不缓存即可。因此,我添加了以下内容:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately 

经过快速测试,我在响应头中得到了正好符合我的期望的结果。

Cache-Control    no-cache, no-store 
Pragma    no-cache 
Expires    -1 

问题:

这个东西上线后,第一天看起来很不错。第二天,嘭,每个人都开始得到白屏和没有PDF显示的情况。经过进一步调查,我发现只有IE 6,7,8有问题。Chrome没问题,Firefox没问题,Safari也没问题,甚至IE 9也没问题。由于不知道为什么会出现这种情况,我撤回了我的更改并部署它,然后一切又正常工作了。

我已经搜索了整个互联网,试图找出为什么我的缓存头好像会让IE 6-8感到困惑,但是没有任何结果。有没有人遇到过这种IE 6-8的问题?我有什么遗漏的吗?谢谢您的任何见解。

1个回答

6
我找到了解决方案。以下是提示我的内容。这里是链接 基本上,如果Cache-Control头部包含“no-cache”或“store-cache”,IE8(及以下版本)会遇到问题。通过只允许私有缓存并将最大年龄设置为非常短,以便它几乎立即过期,我成功解决了这个问题。
//Ie 8 and lower have an issue with the "Cache-Control no-cache" and "Cache-Control store-cache" headers.
//The work around is allowing private caching only but immediately expire it.
if ((Request.Browser.Browser.ToLower() == "ie") && (Request.Browser.MajorVersion < 9))
{
     context.Response.Cache.SetCacheability(HttpCacheability.Private);
     context.Response.Cache.SetMaxAge(TimeSpan.FromMilliseconds(1));
}
else
{
     context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
     context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
     context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately
}

感谢您发布这篇文章。我浪费了很多时间和精力来尝试解决这个问题,非常感到沮丧。 - Adrian Carr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接