CompressFilterAttribute 文件压缩特性
/// <summary>
/// 文件压缩特性
/// </summary>
public class CompressFilterAttribute : ActionFilterAttribute
{
private bool isEnableCompression = true; /// <summary>
/// 构造函数
/// </summary>
public CompressFilterAttribute()
{
} #region OnActionExecuting
/// <summary>
/// Action方法执行前执行的方法
/// </summary>
/// <param name="filterContext">ActionExecutingContext 对象</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionDescriptor.IsDefined(typeof(NoCompress), false) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(NoCompress), false))
{
this.isEnableCompression = false;
}
}
#endregion #region OnResultExecuted
/// <summary>
/// 返回结果集之后执行的方法
/// </summary>
/// <param name="filterContext">结果执行上下文</param>
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
return;
} if (filterContext.Exception != null)
{
return;
} if (this.isEnableCompression)
{
HttpRequestBase request = filterContext.HttpContext.Request;
HttpResponseBase response = filterContext.HttpContext.Response;
string acceptEncoding = request.Headers["Accept-Encoding"]; if (acceptEncoding == null)
{
return;
} if (acceptEncoding.ToLower().Contains("gzip"))
{
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.ToLower().Contains("deflate"))
{
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
response.AppendHeader("Content-Encoding", "deflate");
}
}
#endregion
}
}
文件压缩特性和排除文件压缩特性的完美结合,相信可以让你满足需求。
排除文件压缩特性:NoCompress
/// <summary>
/// 不启用压缩特性
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class NoCompress : Attribute
{
}
CompressFilterAttribute 文件压缩特性的更多相关文章
- Java实现文件压缩与解压
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...
- Java实现文件压缩与解压[zip格式,gzip格式]
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...
- [Swust OJ 138]--文件压缩
题目链接:http://acm.swust.edu.cn/problem/138/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 洛谷P1124 文件压缩
https://www.luogu.org/problem/show?pid=1124 题目背景 提高文件的压缩率一直是人们追求的目标.近几年有人提出了这样一种算法,它虽然只是单纯地对文件进行重排,本 ...
- Huffman的应用之文件压缩与解压缩
文件压缩与解压缩> 近期这段时间一直在学习树的这样的数据结构,也接触到了Huffman树以及了解了什仫是Huffman编码,而我们经常使用的zip压缩也是利用的Huffman编码的特性 ...
- Hadoop文件压缩
1. Hadoop的文件压缩需求 文件压缩对于大容量的分布式存储系统而言是必须的,它能带来两个好处: 1)减少了文件所需的存储空间: 2)加快了文件在网络上或磁盘间的传输速度. 2. Hadoop支持 ...
- bzip2 一种块排序文件压缩软件
总览 bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ] bunzip2 [ -fkvsVL ] [ filenames ... ] bzcat [ - ...
- 开源作品-PHP写的JS和CSS文件压缩利器(单文件绿色版)-SuMinify_PHP_1_5
前言: 网站项目需要引用外部文件以减小加载流量,而且第一次加载外部资源文件后,其他同域名的页面如果引用相同的地址,可以利用浏览器缓存直接读取本地缓存资源文件,而不需要每个页面都下载相同的外部资源文件. ...
- 洛谷 P1124 文件压缩
P1124 文件压缩 题目背景 提高文件的压缩率一直是人们追求的目标.近几年有人提出了这样一种算法,它虽然只是单纯地对文件进行重排,本身并不压缩文件,但是经这种算法调整后的文件在大多数情况下都能获得比 ...
随机推荐
- 黑马程序员 1、C语言32个关键字整理分类
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ C语言一共有32个关键字 一.数据类型关键字(共20个) A.基本数据类型(5个)void :声明 ...
- information_schema.optimizer_trace学习
information_schema.optimizer_trace 用于追踪优化器的优化过程:通常来说这张表中是没有数据的,要想开户追踪要把 @@session.optimizer_trace='e ...
- EF调用存储过程实例
创建实体: public class User { public string UserID { get; set; } public string UserName { get; set; } pu ...
- MIT-scheme安装
下载地址: http://www.gnu.org/software/mit-scheme/ 下载windows版本,安装. The MIT-Scheme can be installed by jus ...
- First Missing Positive 解答
Question Given an unsorted integer array, find the first missing positive integer. For example,Given ...
- OpenWrt compiles
make -r world: build failed. Please re-run make with -j1 V=s to see what's going onmake: *** [world] ...
- java POI读取excel 2007/2003
2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ...
- PHP MySQL Delete From 之 Delete
删除数据库中的数据 DELETE FROM 语句用于从数据库表中删除记录. 语法 DELETE FROM table_name WHERE column_name = some_value 注释:SQ ...
- Python标准库:内置函数classmethod(function)
把类函数当作类的一个方法返回. 类方法第一个參数是指明类,跟类中函数一样,第一个參数是指明类实例. 类方法修饰符採用以下的格式来使用: class C: @classmethod def f(cls, ...
- 用自动化运维工具解放IT运维
何谓自动化运维,即在最少的人工干预下,结合运用脚本与第三方工具,保证业务系统7*24小时高效稳定运行.这应该是所有业务系统运维终极目标. 我们对运维的要求通常是: 1.事前预警 在故障出现之前,管理人 ...