2012-04-03 150 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