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. A Tour of Go For is Go's "while"

    At that point you can drop the semicolons(分号): C's while is spelled for in Go. package main import & ...

  2. STM32F103 与 STM32F407引脚兼容问题

    ===========突袭网收集的解决方案如下=========== 解决方案1: STM32F103有的功能407都有,并且这些功能的引脚完全兼容,只是程序不同而已...而STM32F407有的功能 ...

  3. Oracle ABP(Autotask Background Process)

    ABP相当于自动任务与调度程序之间的中介,其主要作用是将自动任务转换成Autotask作业,供调度程序执行.同样重要的是,ABP还维护所有任务执行的历史记录.ABP将其专用资料档案库存储在sysaux ...

  4. (二)在.net中如何使用Memcached

    Step1:第一步当然是下载Memcached for c# API,

  5. robotframework-FQA

    发现是一波三折,刚开始信步漫游,就又遇上了沟,整理一下吧:  1.WebDriverException: Message: 'geckodriver' executable needs to be i ...

  6. Jquery UI 组合树 - ComboTree 集成Wabacus4.1 代码剖析

    Jquery UI 1.3 (组合树 - ComboTree ) 集成Wabacus4.1 集成Spring 代码剖析 使用时,请下载需要Jquery ui包进行配置 combotree.js 的代码 ...

  7. IMP 导入数据报错 OCI-21500 OCI-22275

    IMP导入数据报错如下: OCI-21500: internal error code, arguments: [kgepop: no error frame to pop to], [], [], ...

  8. [React] React Fundamentals: Using Refs to Access Components

    When you are using React components you need to be able to access specific references to individual ...

  9. Android(java)学习笔记150:为什么局部内部类只能访问外部类中的 final型的常量

    为什么匿名内部类参数必须为final类型: 1)  从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变 ...

  10. Android开发的第一天

    不管做什么开发都是有开始的,对于开发的话开始要的准备的就是开发工具了  安装开发工具配置开发工具好了不多说了现在我来说怎么样安装和配置安卓的开发工具吧 第一首先就是要下载一个JDK (Java SE ...