资源被解释为文档,但使用应用程序/zip MIME类型进行传输。

6

我无法通过Web API的get调用从服务器成功下载文件。下载似乎已经开始,但是Chrome会抛出以下错误:

"Resource interpreted as Document but transferred with MIME type 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
@VAAA 我从未找到解决方案,请在您找到后发布。最终我选择了非流式解决方案,但这远非理想。 - parliament
1个回答

1
您尝试更改请求头,例如 accept 头吗?
此外,在这里 您可以找到一个类似的问题,那里提出的一些解决方案可能会帮助您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接