Reputation: 20643
I have a ASP.NET MVC 4 application with Web API. It's working great. But one problem is that IE can't download a file from a web api while chrome and firefox can. The browser says
"Unable to open this internet site. The requested site is either unavailable or cannot be found."
According to IE 8 and client-side caching, it looks like that no-cache setting causes the problem. So I want to set private cache in that download. But in MVC 4, I found there is no property "Cache" in HttpResponseMessage nor any way to set private cache. Could anybody show how to do this?
Update 1: According to my debugging, it was not cache, but `ContentDisposition' in following code.
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
// response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
// {
// FileName = "PY75.xls"
// };
return response;
If I commented like above, IE can download the file with id as its default file name but it cannot like what described above when uncommenting above. Any idea how to fix this problem? Why IE can't recognize content-disposition header?
Update 2: After upgrading IE 9, content-disposition is finally working and can download from web api.
Upvotes: 3
Views: 5706
Reputation: 6733
I faced a similar problem and blogged about my solution here The jist of the solution is that a Pragma=no-cache header causes downloads to fail in IE 9 and below when on HTTPS. So you basically have to strip off that header. Also, if there's an explicit Cache-control header, and it should have the value as no-store, no-cache as the reverse order would also cause downloads to fail.
Upvotes: 2
Reputation: 4675
Try adding
response.AddHeader("Content-Disposition", "attachment; filename=PY75.xls");
Update
I looked more into this and it may be a result of the way you're opening and closing your streams (not sure without seeing the rest of the code. I tried the following and it worked for me, maybe it will work for you!
public HttpResponseMessage Get()
{
string path = @"PATH_TO_XLS";
MemoryStream responseStream = new MemoryStream();
using (Stream fileStream = File.Open(path, FileMode.Open))
{
fileStream.CopyTo(responseStream);
fileStream.Close();
}
responseStream.Position = 0;
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(responseStream);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "PY75.xls" };
return response;
}
The only time I noticed that the browsers would have issues is when the memory stream was closed before sending the response back, which would make sense as the memory stream has to be open to stream the content back to the client. However I noticed the problem on all browsers not just IE. I'm not sure how the rest of your code is handling that, but if you for example handle creating the stream differently for chunked http vs just sending the whole file that could cause it.
Upvotes: 2