2012-04-03 154 views
3

我有以下代码,部署在HTTPS Asp位点,建立与MVC 4.0:Internet Explorer错误

public FileResult ANotSoWorkingFunction(string filePath, string fileName) 
{ 
pathToFile = string.Format("~/{0}/{1}", pathToFile, fileName); 
return File(new FileStream(pathToFile, FileMode.Open), "application/pdf", fileName); 
} 

这将工作(如你许多你可能已经猜到)与Chrome,Firefox和IE9。但它会抛出:

--------------------------- 
Windows Internet Explorer 
--------------------------- 
Internet Explorer cannot download someFileName from a_site.com. 


Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later. 
--------------------------- 
OK 
--------------------------- 

在IE6,7,8

在这一个任何想法或线索是极大的赞赏,因为我已经花洞一天HTML头打。

编辑:

下面是IE7头:

HTTP/1.1 200 OK 
Cache-Control: private, no-cache="Set-Cookie" 
Content-Type: application/pdf 
Server: Microsoft-IIS/7.5 
X-AspNetMvc-Version: 4.0 
X-AspNet-Version: 4.0.30319 
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly 
X-Powered-By: ASP.NET 
Date: Wed, 04 Apr 2012 08:43:50 GMT 
Content-Length: 233324 

这里是那些从IE9:

HTTP/1.1 200 OK 
Cache-Control: private, no-cache="Set-Cookie" 
Content-Type: application/pdf 
Server: Microsoft-IIS/7.5 
X-AspNetMvc-Version: 4.0 
X-AspNet-Version: 4.0.30319 
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly 
X-Powered-By: ASP.NET 
Date: Wed, 04 Apr 2012 08:42:14 GMT 
Content-Length: 233324 

谢谢

+0

你能告诉我们你的“Cache-Control”和“Set-Cookie”响应头吗? – Levi 2012-04-03 21:27:08

+0

当然可以Lezi,谢谢你在这个 – Calin 2012-04-04 08:49:34

+0

intress可能是在mvc 3相同的问题 http://stackoverflow.com/questions/6943443/asp-mvc3-fileresult-with-accents-ie8-bugged – 2012-04-19 15:01:24

回答

8

我想我也遇到了你的问题。

我也运行IIS 7.5并通过HTTPS请求上的操作下载PDF。由于我还没有找到隔离的原因,IIS 7.5似乎将no-cache="Set-Cookie"添加到我的Cache-Control响应标头中,无论我在响应中设置了哪些缓存设置。这导致了the fairly well documented no-cache issue on IE6, IE7, and IE8

为了解决这个问题,我在FileContentResult上做了一个小的包装器,它清除了名为parent的头,然后将Cacheability设置为'Private'。这侧面加强了IIS 7.5的坚持性,将no-cache="Set-Cookie"添加到头文件中,并在我测试的所有浏览器中正确下载文件。如果你想模仿我所做的,首先,这是我的FileContentResult包装器。

public class PdfContentResult : FileContentResult { 

    public PdfContentResult(byte[] data) : base(data, "application/pdf") { } 

    public PdfContentResult(byte[] data, string fileName) : this(data) { 
     if (fileName == null) { 
      throw new ArgumentNullException("fileName"); 
     } 

     this.FileDownloadName = fileName; 
    } 

    public override void ExecuteResult(ControllerContext context) { 
     context.HttpContext.Response.ClearHeaders(); 

     base.ExecuteResult(context); 

     context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Private); 
    } 
} 

然后,我添加一个扩展方法对我ControllerExtensions因此,这将是简单地发现:

public static class ControllerExtensions { 

    public static PdfContentResult Pdf(this Controller controller, byte[] fileContents, string fileName) { 
     return new PdfContentResult(fileContents, fileName); 
    } 

} 

最后,在操作中,我做的这相当于:

public ActionResult MyGeneratedPdf() { 
    byte[] myPdfContentInByteStream = GetPdfFromModel(); 
    return this.Pdf(myPdfContentInByteStream, "MyFile.pdf"); 
} 

显然,如果您正在下载各种数据类型,您可能不想将解决方法与PDF紧密结合。

+0

听起来很有希望会给它一个尝试,让你知道它是怎么回事 – Calin 2012-07-05 21:23:27

+0

这是否工作? – Technetium 2012-08-10 23:19:04

+0

是的,它的工作原理非常好,谢谢 – Calin 2012-08-13 12:50:56

0

在老版本的IE浏览器如果用户试图通过HTTPS连接下载文件,则会阻止缓存的任何响应标头导致文件下载过程失败。以下是这是导致该问题最常见的标题:

  • 的Cache-Control与值无缓存或无店铺
  • 因人而异任意值
  • 附注值为无缓存

您可以创建一个ActionFilterAttribute这将清除缓存头为您这样的:

public class ClearCacheHeadersAttribute : FilterAttribute, IActionFilter 
{ 
    public void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     return; 
    } 

    public void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     HttpContext.Current.Response.Headers.Remove("Cache-Control"); 
     HttpContext.Current.Response.Headers.Remove("Vary"); 
     HttpContext.Current.Response.Headers.Remove("Pragma"); 

     //Set the cache headers any way you like keeping in mind which values can brake the download 
    } 
} 

和装饰哟我们的动作:

[ClearCacheHeaders] 
public FileResult ANotSoWorkingFunction(string filePath, string fileName) 
{ 
    pathToFile = string.Format("~/{0}/{1}", pathToFile, fileName); 
    return File(new FileStream(pathToFile, FileMode.Open), "application/pdf", fileName); 
} 
+0

谢谢你的回答,我昨天在我的搜索中尝试了这个,但没有运气 – Calin 2012-04-04 08:46:54

+0

由于某些原因,它仍然会添加Cache-Control标头 – Calin 2012-04-04 08:54:26

+0

请检查您的Web.config或IIS中的configuration/system.webServer/staticContent节点配置文件。您也可以尝试将Cache-Control设置为某个允许的值,例如:HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public); – tpeczek 2012-04-04 09:45:13

1

我们通过在流式传输文件之前更改缓存控制标头来解决此问题。

简化代码示例:

var browserInformation = Request.Browser; 

//Set as private if current browser type is IE 
Response.AppendHeader("cache-control", 
        browserInformation.Browser == "IE" ? "private" : "no-cache"); 

return File(fileName, contentType, downloadFileName); 

这个工作(耶)..但我留下了为什么我们要做这种方式针对特定的网站缺乏明确的。我们有四个网站在同一个盒子上运行,全部都在SSL下,只有一个网站有这个问题。我比较了web.config文件并查看了IIS中的设置,但无法进一步阐明为什么一个站点需要明确设置这些标头。

如果任何人有更多的添加上述(为增加claird),这将是伟大的。

+0

Web服务器是否运行不同版本的IIS?我的问题似乎是特定于IIS 7.5的。 – Technetium 2012-06-29 19:34:52