资源解释为Document但使用MIME类型application / zip传输:

时间:2013-12-16 22:47:23

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

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

“资源被解释为文档但使用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=?=

1 个答案:

答案 0 :(得分:1)

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

此外,here您可以找到类似的问题,其中一些解决方案可能会对您有所帮助。

相关问题