C# Asp.NETMVC4API:下载docx在IE8中失败

C# Asp.NETMVC4API:下载docx在IE8中失败,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我将文档存储在数据库中,并有一个用于下载文档的api docx和xlsx的下载在IE9、Chrome和FF中运行良好,但在真正的IE8中失败。(IE8模式下的IE9也可以运行) 我收到的错误消息如下: 无法从idler2下载393 无法打开此Internet站点。请求的站点为 不可用或找不到。请稍后再试 使用以下响应标题: HTTP/1.1200ok 缓存控制:没有缓存 Pragma:没有缓存 Content-Length: 10255 Content-Type: application/oct

我将文档存储在数据库中,并有一个用于下载文档的api

docx和xlsx的下载在IE9、Chrome和FF中运行良好,但在真正的IE8中失败。(IE8模式下的IE9也可以运行)

我收到的错误消息如下:

无法从idler2下载393

无法打开此Internet站点。请求的站点为 不可用或找不到。请稍后再试

使用以下响应标题: HTTP/1.1200ok 缓存控制:没有缓存 Pragma:没有缓存

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
这是我的api方法:

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;
}
public HttpResponseMessage GetDocumentContent(int-id)
{
Document Document=Repository.StorageFor().GetDocument(id);
HttpResponseMessage response=Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content=newbytearraycontent(document.GetBuffer());
response.Content.Headers.ContentLength=document.GetBuffer().Length;
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{
//FileName=document.GetFileName(),
FileName=“document.docx”,
DispositionType=“附件”
};
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
返回响应;
}

我在内容配置和内容标题上尝试了很多变化,但运气不好

到目前为止,我找到的唯一解决办法是将文件存储在临时文件夹中并返回下载url。然后(javascript)客户端可以打开一个新窗口


不是很好,但是MVC4API似乎带来了一些限制

我假设您在SSL下会遇到这种情况。如果是这样的话,这就是一个问题。本文讨论的是Office文档,但这个问题适用于所有文件类型

那篇文章的解决方案是删除无缓存标题,但还有更多内容。当IE8通过SSL与网站通信时,IE8强制执行任何无缓存请求。如果存在一个或多个头,IE8不会缓存文件。因此,它无法打开该文件。所有这些都是针对IE5到IE8的

在MVCWebAPI中,它实际上需要另一个步骤。由于要创建新的HttpResponseMessage,还必须在消息头上创建CacheControlHeaderValue。您不必设置任何头属性,只需实例化一个新的头属性即可。标题将默认为所需内容,因此您不必更改属性

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;
}
public HttpResponseMessage GetDocumentContent(int-id)
{
Document Document=Repository.StorageFor().GetDocument(id);
HttpResponseMessage response=Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Headers.CacheControl=new CacheControlHeaderValue();//必需
response.Content=newbytearraycontent(document.GetBuffer());
response.Content.Headers.ContentLength=document.GetBuffer().Length;
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{
FileName=“document.docx”
};
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
返回响应;
}

我有一个确切的问题,但这就解决了。

马上!非常感谢你。+1:解决了问题,用你的答案解决了问题,但还有一句话:它体现在IE 11、Google Chrome和Firefox 43.0.4中,但它取决于我们请求的PDF大小:发现限制在50 KB左右。。。基于大小的不同缓存策略!?