I'm storing documents in a database and have an api for downloading documents.
The downloading of docx and xlsx works fine in IE9,Chrome and FF but fails in a real IE8.(IE 9 in IE8 mode also works)
The error message I get is the following:
Unable to download 393 from idler2.
Unable to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
With the following response header: HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache
Content-Length: 10255
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename=document.docx
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 23 Mar 2013 11:30:41 GMT
This is my api method:
public HttpResponseMessage GetDocumentContent(int id)
{
Document document = Repository.StorageFor<Client>().GetDocument(id);
HttpResponseMessage response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content = new ByteArrayContent(document.GetBuffer());
response.Content.Headers.ContentLength = document.GetBuffer().Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
//FileName = document.GetFileName(),
FileName = "document.docx",
DispositionType = "attachment"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}
I've tried quite a few variations on the content disposition and content header, but without luck..
I will assume you experience this under SSL. If so then this is a known issue. This article talks about Office documents but this problem applies to all file types.
That article's resolution is to remove the no-cache header but there is more to it. When IE8 communicates with the Web site through SSL, IE8 enforces any no-cache request. If the header or headers are present, IE8 does not cache the file. Consequently it cannot open the file. All this is specific to IE5 to IE8.
In MVC Web API it actually takes another step. Since you are creating a new HttpResponseMessage you also have to create a CacheControlHeaderValue on the message's header. You shouldn't have to set any header properties, just instantiate a new one. The headers will default to what is needed so you shouldn't have to change the properties.
public HttpResponseMessage GetDocumentContent(int id)
{
Document document = Repository.StorageFor<Client>().GetDocument(id);
HttpResponseMessage response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Headers.CacheControl = new CacheControlHeaderValue(); // REQUIRED
response.Content = new ByteArrayContent(document.GetBuffer());
response.Content.Headers.ContentLength = document.GetBuffer().Length;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "document.docx"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return response;
}
I had the exact problem but this solved it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With