HttpHandler:处理请求(Request)的信息和发送响应(Response)。
HttpModule:通过Http Module向Http请求输出流中写入文字,httpmodule先执行

它们两个的区别:
页面处理程序在处理过程中,要经历HttpModule,HttpHandler的处理HttpModule用于页面处理前和处理后的一些事件的处理,HttpHandler进行真正的页面的处理。
HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版 权声明)等。

IHttpModule:是属于大小通吃类型,无论客户端请求的是什么文件,都会调用到它;例如aspx,rar,html的请求.

IHttpHandler:则属于挑食类型,只有ASP.net注册过的文件类型(例如aspx,asmx等等)才会轮到调用它.

案例:在图片指定位置上打印指定文字 (以下是 ashx程序)
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 实现文件下载

如果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();
WebClient 以流方式下载
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及实现文件下载的更多相关文章

  1. httphandler和httpmodule的区别

    ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...

  2. 选择HttpHandler还是HttpModule?

    阅读目录 开始 理解ASP.NET管线 理解HttpApplication 理解HttpHandler 理解HttpModule 三大对象的总结 案例演示 如何选择? 最近收到几个疑问:HttpHan ...

  3. (转)HttpHandler与HttpModule的理解与应用

    神秘的HttpHandler与HttpModule 大学时候我是从拖控件开始学习 asp.net的,对.net的很多类库对象都不是很了解.所以看到大家写一些个性的asp.net名词,就感觉asp.ne ...

  4. 3.httphandler和httpmodule各种的作用以及工作原理?

    首先应该知道的是ASP.NET 请求处理过程是基于管道模型的,这个管道模型是由多个HttpModule和HttpHandler组成,ASP.NET 把http请求依次传递给管道中各个HttpModul ...

  5. 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写

    回到目录 说在前 对于资源列表页来说,我们经常会把图像做成N多种,大图,小图,中图等等,很是麻烦,在数据迁移时,更是一种痛快,而如果你把图像资源部署到nginx上,那么这种图像缩放就变得很容易了,因为 ...

  6. HttpHandler与HttpModule的用处与区别

    问题1:什么是HttpHandler? 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什么时候使用HttpModule? 答案1:HttpHandler,Http ...

  7. httphandler与httpmodule区别

    1.配置不同: <httpModules> <!--name表示类名,BtEd2k.UILogic表示命名空间--> <add name="Common&quo ...

  8. ASP.NET内部原理(HttpHandler和HttpModule)

    [IT168 技术文档]在以前的ASP时候,当请求一个*.asp页面文件的时候,这个HTTP请求首先会被一个名为 inetinfo.exe进程所截获,这个进程实际上就是www服务.截获之后它会将这个请 ...

  9. HttpHandler与HttpModule介绍

    前言:作为一个开发人员,我们看过很多的关于开发的书,但是都是教我们"知其然",并没有教我们"知其所以然",我们开发web项目的过程中,当我们输完URL敲下回车就 ...

随机推荐

  1. 最短路变形 poj3615&

    问题: 牛要跨过一些障碍,希望以最小的体力跨过障碍,并且对于一条路径,只在乎其中最高的障碍. 输入N代表站点数,标记为1—N,输入M代表路径数,从站点S到E之间需要跨过高度为H的障碍. 输入T代表牛要 ...

  2. vue相关

    勾三股四的vue+webpack实战:http://jiongks.name/blog/just... 用Vue构建一个Notes App:https://coligo.io/learn-vuex-. ...

  3. PAT 07-图6 旅游规划 (25分)

    有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便 ...

  4. linux命令之nohup

    功能: 使进程在退出登录后仍旧继续执行,nohup就是不挂起的意思(no hang up). 格式:$nohup command 和 $nohup command & 两种,二者之间的区别就是 ...

  5. "用wow64exts调试64位任务管理器抓取的32位程序的dump"

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:"用wow64exts调试64位任务管理器抓取的32位程序的dump".

  6. 树莓派通过 HDMI - VGA 转接后分辨率始终为640*480无法修改的问题

    一开始装的Raspbian,感觉系统不错,就是分辨率调不了,网上找了很多解决方法,捣鼓了差不多一天,仍然没有解决. 期间尝试换了好几个系统,比如说 raspbmc .XBian等,最后试了下Pidor ...

  7. Redis: OOM command not allowed when used memory > ‘maxmemory

    Redis: OOM command not allowed when used memory > ‘maxmemory’ 解决方式: $ vim /etc/redis/6903.conf ma ...

  8. 错误: 找不到或无法加载主类 / Class not found

    Java Resources文件上有红色感叹号存在 说明引入jar包存在错误,把引用错误的jar包去掉即可. 右键项目,Properties,Java Build Path,选中jar包remove, ...

  9. js 字符及字符串

    1. 判断是否为null或者空字符 var == null var == undefined var == '' 2. 字符串及其分割 var arr = new Array(); //定义一数组 a ...

  10. php笔记08:数据库编程---使用php的MySQL扩展库操作MySQL数据库

    1.使用php的MySQL扩展库操作MySQL数据库: php有3种方式操作MySQL数据库 (1)mysql扩展库 (2)mysqli扩展库 (3)pdo     mysql扩展库与mysql数据库 ...