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. Codeforces Round #341 (Div. 2) ABCDE

    http://www.cnblogs.com/wenruo/p/5176375.html A. Wet Shark and Odd and Even 题意:输入n个数,选择其中任意个数,使和最大且为奇 ...

  2. CodeForces 352C. Jeff and Rounding(贪心)

    C. Jeff and Rounding time limit per test:  1 second memory limit per test: 256 megabytes input: stan ...

  3. ldconfig及 LD_LIBRARY_PATH

    dconfig及 LD_LIBRARY_PATH 1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library ...

  4. win32键盘记录 -- 自定义窗口类

    最近学了些关于window api编程的知识,于是琢磨编写一些键盘记录器,能够把输入的按键输出到窗口内,并且实现窗口自动滚动. 封装窗口类使用了GWL_USERDATA字段来保存this指针,比较容易 ...

  5. 前端javascript规范文档 (http://www.xuanfengge.com/category/web)

    说明:本文档为前端JS规范 一.规范目的 为提高团队协作效率,便于前端后期优化维护,输出高质量的文档. 二.基本准则 符合web标准,结构表现行为分离,兼容性优良.页面性能方面,代码要求简洁明了有序, ...

  6. 数字信号处理与音频处理(使用Audition)

    前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...

  7. 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求(转)

    转自:http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html 今天下午17点左右,博客园博客站点出现这样的错误信息: Error Sum ...

  8. Tomcat 优化

    1.apr 许多朋友可能在启动tomcat的时候都会看到类似这样的信息: 引用 org.apache.catalina.core.AprLifecycleListener init 信息: The A ...

  9. 自定义HttpHandler配置iis8.0

    配置环境,window8 ,iis8.0,.net Framework4.0,托管管道模式“经典” 步骤 (1)进入iis管理器,选中要设置的网站-->功能视图-->MIME类型--> ...

  10. mysql strace fsync,fdatasync

    mysql> show create table y; +-------+------------------------------------------------------------ ...