HttpHandler与HttpModule及实现文件下载
HttpHandler:处理请求(Request)的信息和发送响应(Response)。
HttpModule:通过Http Module向Http请求输出流中写入文字,httpmodule先执行
它们两个的区别:
页面处理程序在处理过程中,要经历HttpModule,HttpHandler的处理HttpModule用于页面处理前和处理后的一些事件的处理,HttpHandler进行真正的页面的处理。
HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版 权声明)等。
IHttpModule:是属于大小通吃类型,无论客户端请求的是什么文件,都会调用到它;例如aspx,rar,html的请求.
IHttpHandler:则属于挑食类型,只有ASP.net注册过的文件类型(例如aspx,asmx等等)才会轮到调用它.
context.Response.ContentType = "image/JPEG";
string name = context.Request["Name"]; //url的请求参数
string fullpath = HttpContext.Current.Server.MapPath("20110410231802.jpg"); //指定图片路径
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawString(name, new System.Drawing.Font("黑体", ), System.Drawing.Brushes.Pink, , ); //设置参数
}
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
如果HttpHander输出的是html,txt,jpeg等类型的信息,那么浏览器会直接显示,如果希望弹出保存对话框
需要Header添加:Content-Disposition:attachment;filename=自定义.jpg
context.Response.ContentType = "image/JPEG";
string name = HttpUtility.UrlEncode("哈哈.jpg"); // URL编码,防止乱码
context.Response.AddHeader("Content-Disposition","attachment;filename=" + name); // 添加Hander
context.Response.WriteFile("20110410231802.jpg"); // 输出文件
然后在HTML页面中调用 <a href="down.asxh">图片下载</a>
//文件下载
protected void imgGet_Click(object sender, ImageClickEventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
if (File.Exists(Server.MapPath(filePath)))
{
FileInfo file = new FileInfo(Server.MapPath(filePath));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
另一种差不多的方法实现下载
// 实现文件下载
string filePath = Soft.Rows[]["AppAdd"].ToString();
string fileName = Soft.Rows[]["AppName"].ToString(); FileInfo info = new FileInfo(Server.MapPath(filePath));
long fileSize = info.Length;
Response.Clear();
Response.ContentEncoding = Encoding.GetEncoding("UTF-8"); //解决中文乱码
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName + info.Extension)); //解决中文文件名乱码
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, , fileSize);
Response.Flush();
Response.Close();
string url = "http://192.168.8.53:808/test.mp3"; WebClient web = new WebClient();
byte[] arr = web.DownloadData(url);
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("test.mp3", Encoding.UTF8));
context.Response.AddHeader("Content-Length", arr.Length.ToString());
context.Response.BinaryWrite(arr);
context.Response.End();
context.Response.Close();
HttpHandler与HttpModule及实现文件下载的更多相关文章
- httphandler和httpmodule的区别
ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...
- 选择HttpHandler还是HttpModule?
阅读目录 开始 理解ASP.NET管线 理解HttpApplication 理解HttpHandler 理解HttpModule 三大对象的总结 案例演示 如何选择? 最近收到几个疑问:HttpHan ...
- (转)HttpHandler与HttpModule的理解与应用
神秘的HttpHandler与HttpModule 大学时候我是从拖控件开始学习 asp.net的,对.net的很多类库对象都不是很了解.所以看到大家写一些个性的asp.net名词,就感觉asp.ne ...
- 3.httphandler和httpmodule各种的作用以及工作原理?
首先应该知道的是ASP.NET 请求处理过程是基于管道模型的,这个管道模型是由多个HttpModule和HttpHandler组成,ASP.NET 把http请求依次传递给管道中各个HttpModul ...
- 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写
回到目录 说在前 对于资源列表页来说,我们经常会把图像做成N多种,大图,小图,中图等等,很是麻烦,在数据迁移时,更是一种痛快,而如果你把图像资源部署到nginx上,那么这种图像缩放就变得很容易了,因为 ...
- HttpHandler与HttpModule的用处与区别
问题1:什么是HttpHandler? 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什么时候使用HttpModule? 答案1:HttpHandler,Http ...
- httphandler与httpmodule区别
1.配置不同: <httpModules> <!--name表示类名,BtEd2k.UILogic表示命名空间--> <add name="Common&quo ...
- ASP.NET内部原理(HttpHandler和HttpModule)
[IT168 技术文档]在以前的ASP时候,当请求一个*.asp页面文件的时候,这个HTTP请求首先会被一个名为 inetinfo.exe进程所截获,这个进程实际上就是www服务.截获之后它会将这个请 ...
- HttpHandler与HttpModule介绍
前言:作为一个开发人员,我们看过很多的关于开发的书,但是都是教我们"知其然",并没有教我们"知其所以然",我们开发web项目的过程中,当我们输完URL敲下回车就 ...
随机推荐
- Windows 服务 创建 和 安装 -摘自网络
What a Windows Service is Enables you to create long-running executable applications that run in the ...
- POJ2923--Relocation(01背包+状压dp)
果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Mem ...
- MongoDB 入门之查询(find)
MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...
- epoll原理解释(转)
转自:http://yaocoder.blog.51cto.com/2668309/888374 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. ...
- C#性能优化的一些技巧
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:C#性能优化的一些技巧.
- Repository模式介绍汇总
1.Linq To Sql中Repository模式应用场景 http://www.cnblogs.com/zhijianliutang/archive/2012/02/24/2367305.html ...
- Android Studio中使用Gradle打包
首先要注意一点,Android Studio中把proguard.txt已经命名为proguard-rules.pro,由此可见,採用Gradle打包,混淆规则文件的名称是不重要的.能够自己随便命名. ...
- jfinal上传下载
1. 上传 <form id="form1" method="post" enctype="multipart/form-data" ...
- jqgrid表格列动态加载的实现
选中几个测点名,在表格中就显示几列. 具体代码如下: function reloadGrid(postData){ $('#gridTable').jqGrid('GridUnload'); var ...
- careercup-递归和动态规划 9.9
9.9 设计一种算法,打印八皇后在8*8棋盘上的各种摆法,其中每个皇后都不同行.不同列,也不在对角线上.这里的“对角线”指的是所有的对角线,不只是平分整个棋盘的那两条对角线. 类似leetcode:N ...