title: "Content decoding has failed" error when download files using GZIP compression
body: <p>My web app generates a CSV file on the fly, but whenever I use GZIP compression, the download fails:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The download appears as "Interrupted" in Google Chrome, and in Internet Explorer appears an error that says "Content decoding has failed" .</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The solution is disabling compression on that action, but... why does this happen?</p>
  
  <p>Cheers.</p>
  
  <p><strong>UPDATE:</strong> The compression filter that I use:</p>
  
  <pre><code>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 &amp;&amp; actionAttributes.Any(attr =&gt; 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");
          }
      }
  }
  </code></pre>