【问题标题】:Asp.Net MVC 4 API: Download of docx failing in IE8Asp.Net MVC 4 API:在 IE8 中下载 docx 失败
【发布时间】:2013-03-23 11:37:38
【问题描述】:

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

docx 和 xlsx 的下载在 IE9、Chrome 和 FF 中可以正常下载,但在真正的 IE8 中会失败。(IE 9 在 IE8 模式下也可以)

我得到的错误信息如下:

无法从idler2下载393。

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

带有以下响应标头: HTTP/1.1 200 正常 缓存控制:无缓存 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;
}

我在内容配置和内容标题上尝试了很多变体,但都没有运气..

【问题讨论】:

    标签: c# asp.net-mvc-4


    【解决方案1】:

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

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

    在 MVC Web API 中,它实际上又迈出了一步。由于您正在创建一个新的 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;
    }
    

    我遇到了确切的问题,但这解决了它。

    【讨论】:

    • 看准了!非常感谢。
    • +1:有问题,用你的答案解决了,但还有一点:它在 IE 11、Google Chrome 和 Firefox 43.0.4 中表现出来,但它取决于 PDF 的大小我们要求:发现限制约为 50 KB...根据大小不同的缓存策略!?
    【解决方案2】:

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

    不是很好,但似乎 MVC 4 API 带来了一些限制。

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多