【问题标题】:Resource interpreted as Document but transferred with MIME type application/zip:资源解释为 Document,但使用 MIME 类型 application/zip 传输:
【发布时间】:2013-12-16 22:47:23
【问题描述】:

我无法使用 Web API get 调用从服务器成功下载文件。下载似乎开始了,但随后 Chrome 抛出:

“资源被解释为 Document,但使用 MIME 类型 application/zip 传输”

Firefox 没有这么说,但下载仍然失败。

我在以下设置中做错了什么?:

    [HttpGet, Route("api/extractor/downloadresults")]
    public HttpResponseMessage DownloadResultFiles()
    {
        int contentLength = 0;
        this.ResultFiles.ForEach(f => contentLength = contentLength + f.FileSize);

        var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
        {
           ...zip files...
        });

        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        streamContent.Headers.ContentLength = contentLength;
        streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "result.zip"
        };

        var response = Request.CreateResponse();

        response.StatusCode = HttpStatusCode.OK;
        response.Content = streamContent;
    }

我通过以下方式触发下载:

  window.location.href = "api/extractor/downloadresults";

使用生成的标题:

请求标头

  Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Cookie:ASP.NET_SessionId=ibwezezeutmu2gpajfnpf41p
  Host:localhost:47384
  Referer:http://localhost:47384/
  User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36

响应标头

  Cache-Control:no-cache
  Content-Disposition:attachment; filename=result.zip
  Content-Length:436102
  Content-Type:application/zip
  Date:Mon, 16 Dec 2013 22:36:31 GMT
  Expires:-1
  Persistent-Auth:true
  Pragma:no-cache
  Server:Microsoft-IIS/8.0
  X-AspNet-Version:4.0.30319
  X-Powered-By:ASP.NET
  X-SourceFiles:=?UTF-8?B?QzpcbmV3VG9vbGJveFxUb29sYm94XFRvb2xib3guV2ViXGFwaVx0ZXJtZXh0cmFjdG9yXGRvd25sb2FkcmVzdWx0ZmlsZXM=?=

【问题讨论】:

  • 嗨,对不起,我有同样的问题。解决此警告的解决方案是什么?谢谢
  • @VAAA 我从来没有找到解决方案,如果你找到了,请发布。我最终选择了一个非流媒体解决方案,但这远非理想。

标签: asp.net-mvc download mime-types asp.net-web-api


【解决方案1】:

您是否尝试过更改请求标头,例如接受标头?

另外,here 你可以找到一个类似的问题,那里建议的一些解决方案可能会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 2017-03-19
    • 2013-05-26
    • 2017-05-12
    • 2017-07-10
    • 2023-03-12
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多