"文件无法下载"在Internet Explorer中的ASP.NET MVC

问题描述:

所以我返回 FileContentResult 从这样一个动作:

So I'm returning a FileContentResult from an action like this:

return File(pck.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "MyExcelFile.xlsx");

当点击IE浏览器中的打开(我使用IE9,版本9.0.8112.16421),它说:文件无法下载,用户是一个重试按钮psented $ P $。如果他们点击重试,它工作正常。如果他们点击保存,它工作正常。在Firefox中,它工作正常。

When clicking "Open" in IE (I'm using IE9, version 9.0.8112.16421), it says "File couldn't be downloaded" and the user is presented with a 'Retry' button. If they click Retry, it works fine. If they click Save, it works fine. In Firefox, it works fine.

我怎样才能让用户打开该文件时,点击打开第一次?

How can I allow the user to open the file when the click Open the first time?

我有同样的问题,无法提供很好的解决方案(除了什么Tood建议,其中的选项)。但是寻找与小提琴手和放大器的情况; CO。,我有一些可能会有所帮助的更多信息。

I have the same problem and cannot offer a good solution (besides what Tood suggests, which is an option). But looking at the situation with fiddler & co., I have some more information that may be of help.

我们的应用程序动态创建PDF文档,并提供它们作为下载。这个问题显然是依赖于数据的,这意味着有些生成的文件下载罚款第一次尝试,而其他重复性需要重试。

Our application is creating PDF documents on the fly and offering them as downloads. The issue is clearly data-dependent, meaning that some generated files download fine on the first attempt while others reproducibly need the retry.

提琴手显示服务器的响应是相同的每次访问,据我可以告诉。然而,要求不同,(样品稍微改动过):

Fiddler shows the server responses to be identical on each access, as far as I can tell. The requests differ, however (samples slightly edited):

第一个请求:

GET http://localhost:12345/Item/PDF/id HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: ...
Accept-Language: ...
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: localhost:12345
Connection: Keep-Alive
Cookie: ASP.NET_SessionId=52znkt1fcisrolj44tnuyzu4

第二个请求:

GET http://localhost:12345/Item/PDF/id HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:12345
Connection: Keep-Alive
Cookie: ASP.NET_SessionId=52znkt1fcisrolj44tnuyzu4

请注意,第二个请求如何降低接受:头刚 * / * 。我是不愿意的理由文件扩展名添加到URL的是,建议下载名称从项目数据生成,提交与响应否则完全无关的ID。

Note, how the second request reduces the 'Accept:' header to just */*. The reason that I'm reluctant to add a file extension to the Url is that the suggested download name is generated from item data, submitted with the response and otherwise totally unrelated to the ID.