“内容解码失败”使用 GZIP 压缩下载文件时出错
我的网络应用程序动态生成 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");
}
}
}
My web app generates a CSV file on the fly, but whenever I use GZIP compression, the download fails:
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
The download appears as "Interrupted" in Google Chrome, and in Internet Explorer appears an error that says "Content decoding has failed" .
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
The solution is disabling compression on that action, but... why does this happen?
Cheers.
UPDATE: The compression filter that I use:
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");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
另一个问题可能是缓存:如果一个客户端接受压缩内容,但下一个客户端不接受,而服务器缓存了压缩数据怎么办?第二个客户会得到什么?无法解码的缓存压缩页面!
要解决这个问题,请添加另一个方法:
Try:
Another problem may be the caching: what if a client accepts compressed content, but the next client doesn't, and the server cached the compressed data? what the second client will get? A cached compressed page that won't decode!
To fix that, add another method: