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

Matt Grande picture Matt Grande · Mar 7, 2012 · Viewed 9.1k times · Source

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?

Answer

Todd Sprang picture Todd Sprang · Feb 13, 2013

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!