http - 使用GZIP压缩下载文件时出现"Content decoding has failed"错误

标签 http gzip download http-compression

我的网络应用程序会即时生成一个 CSV 文件,但每当我使用 GZIP 压缩时,下载都会失败:

HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0,no-store, no-cache
Transfer-Encoding: chunked
Content-Type: text/csv;charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
Content-Disposition: attachment;filename="filename.csv"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
p3p: CP="CAO PSA OUR"
Date: Fri, 03 Feb 2012 11:27:27 GMT

下载在 Google Chrome 中显示为“已中断”,在 Internet Explorer 中显示为“内容解码失败”的错误。

HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0,no-store, no-cache
Transfer-Encoding: chunked
Content-Type: text/csv;charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
Content-Disposition: attachment;filename="filename.csv"
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
p3p: CP="CAO PSA OUR"
Date: Fri, 03 Feb 2012 11:23:30 GMT

解决方案是禁用对该操作的压缩,但是......为什么会发生这种情况?

干杯。

更新:我使用的压缩过滤器:

public class EnableCompressionAttribute : ActionFilterAttribute
{
    const CompressionMode compress = CompressionMode.Compress;
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.IsChildAction)
            return;

        var actionAttributes = filterContext.ActionDescriptor.GetCustomAttributes(true);
        if (actionAttributes != null && actionAttributes.Any(attr => attr is SkipCompressionAttribute))
            return;

        HttpRequestBase request = filterContext.HttpContext.Request;
        HttpResponseBase response = filterContext.HttpContext.Response;
        String acceptEncoding = request.Headers["Accept-Encoding"];

        if (acceptEncoding == null || response.Filter == null)
            return;

        if (acceptEncoding.ToLower().Contains("gzip"))
        {
            response.Filter = new GZipStream(response.Filter, compress);
            response.AppendHeader("Content-Encoding", "gzip");
            response.AppendHeader("Vary", "Accept-Encoding");
        }
        else if (acceptEncoding.ToLower().Contains("deflate"))
        {
            response.Filter = new DeflateStream(response.Filter, compress);
            response.AppendHeader("Content-Encoding", "deflate");
            response.AppendHeader("Vary", "Accept-Encoding");
        }
    }
}

最佳答案

尝试:

Response.Headers.Remove("Content-Encoding");
Response.AppendHeader("Content-Encoding", "gzip");

另一个问题可能是缓存:如果一个客户端接受压缩内容,但下一个客户端不接受,而服务器缓存了压缩数据怎么办?第二个客户会得到什么?无法解码的缓存压缩页面!

要解决这个问题,请添加另一种方法:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "GZIP")
    {
        string acceptEncoding = HttpContext.Current.Response.Headers["Content-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding))
            return "";
        else if (acceptEncoding.Contains("gzip"))
            return "GZIP";
        else if (acceptEncoding.Contains("deflate"))
            return "DEFLATE";

        return "";
    }

    return base.GetVaryByCustomString(context, custom);
}

关于http - 使用GZIP压缩下载文件时出现"Content decoding has failed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128045/

相关文章:

android - Flutter: InternalLinkedHashMap<String, dynamic >' has no instance method ' cast' with matching arguments

node.js - Nodejs express : compression of response doesn't work

browser - 浏览器如何在下载中找出文件名?

java - HttpClient POST 获取 html 页面的速度比浏览器慢约 2.5 倍

java - http get 返回 403

javascript - NodeJS : zlib. Gunzip( body ) 错误地返回 `undefined`

android - IntentService android下载并返回文件到Activity

objective-c - 在制作保存文章的RSS阅读器时,如何防止重复?

javascript - 来自 Angular 的 HTTP 请求作为 OPTIONS 而不是 POST 发送

Python Twitter 工具和 gzip 错误 "IOError: CRC check failed"