2011-12-01 46 views
7

故事背景:IE 8和客户端缓存

我有一个IIS 6 Web服务器上在.NET 3.5的门户网站。目前有一个页面给出了一个值,并基于该值在Web服务上查找PDF文件,并在网页的另一个选项卡中将结果显示给用户。这是用下面的代码完成的。

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作为相同的PDF返回,直到他们清除临时Internet缓存。

我觉得很酷,这是一个容易的。我只是将缓存标头添加到响应中以永不缓存它。因此,我增加了以下内容:

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?有什么我失踪?感谢您的任何见解。

回答

6

我找到了解决方案。这是给我的启示。 Here is a link

基本上IE8(和更低版本)在缓存控制标头上有问题,如果它有no-cachestore-cache。我能够通过基本允许私有缓存来解决问题,并将最大年龄设置得很短,因此几乎立即过期。

//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 
} 
+0

感谢您发布此信息。我浪费了很多时间和很多挫折试图弄清楚。 –