1 using System; 2 using System.Collections.Generic; 3 using System.IO.Compression; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7 8 namespace PaiXie.Pos.Admin { 9 /// <summary> 10 /// 文件压缩 11 /// </summary> 12 public class CompressFilter : ActionFilterAttribute { 13 public override void OnActionExecuting(ActionExecutingContext filterContext) { 14 HttpRequestBase request = filterContext.HttpContext.Request; 15 16 string acceptEncoding = request.Headers["Accept-Encoding"]; 17 18 if (string.IsNullOrEmpty(acceptEncoding)) return; 19 20 acceptEncoding = acceptEncoding.ToUpperInvariant(); 21 22 HttpResponseBase response = filterContext.HttpContext.Response; 23 24 if (acceptEncoding.Contains("GZIP")) { 25 response.AppendHeader("Content-encoding", "gzip"); 26 response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); 27 } 28 else if (acceptEncoding.Contains("DEFLATE")) { 29 response.AppendHeader("Content-encoding", "deflate"); 30 response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); 31 } 32 } 33 } 34 }