“File couldn't be downloaded” in Internet Explorer with ASP.NET MVC

╄→гoц情女王★ 提交于 2019-12-12 10:50:13

问题


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

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

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?


回答1:


I was able to "trick" IE into doing the right thing by modifying the URL. It's kind of hacky, but here's the detail. HTH.

As a good MVC coder, I used Url.Action() to generate the right link in my view to my controller action. The result was "/Subscription/DownloadArchive", and I had the same issue. (I'm streaming a ZIP file down, but it seems no different than your CSV.) On a whim just now, after reading your post, I hard-coded the URL to "/Subscription/DownloadArchive/Archive.zip". I ignore the "Archive.zip" in code, but that is in fact the file name I return from my controller's action.

Presto!




回答2:


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.

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):

First request:

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

Second request:

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

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.




回答3:


I had the same problem but if I changed the port number of Visual Studio Development Server to another, then this problem disappeared.




回答4:


this works..

Response.Clear();
Response.ClearHeaders();
Response.ClearContent(); 
Server.ScriptTimeout = 3000;
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + fileName);
Response.ContentType = "application/x-msdownload";
excelFile.SaveXls(Response.OutputStream);  
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
// Response.Close();
Response.End();


来源:https://stackoverflow.com/questions/9609837/file-couldnt-be-downloaded-in-internet-explorer-with-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!