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敲下回车就 ...
随机推荐
- weekend110(Hadoop)的 第一天笔记
(2015年1月10日) 课程目录 01-hadoop职位需求状况 02-hadoop课程安排 03-hadoop应用场景 04-hadoop对海量数据处理的解决思路 05-hadoop版本选择和伪分 ...
- redis的hash, list, set类型相关命令
hash相关命令: 1. hset HSET key field value 将哈希表key中的域field的值设为value.如果key不存在,一个新的哈希表被创建并进行hset操作.如果域fiel ...
- PC-红警联机问题与下载
或许不是软件问题: 你做好相关设置了吗? 红警局域网联机的具体方法: 适用于原版红警.尤里复仇,及任何同样的扩展版. 第一步:安装IPX协议. 方法: 控制面板——网络连接(或网上邻居·属性)——本地 ...
- 第一章 Windows NT System Components
Page 3. The focus(焦点) of this book is Windows NT file system and the interaction(交互) of the file sys ...
- 解决libpython2.6.so.1.0: cannot open shared object file
文章解决的问题:安装nginx中需要Python2.6的支持,下面介绍如何安装Python2.6,并建立lib的连接. 问题展示:error while loading shared librarie ...
- android ORMlite的应用
ORMLite -轻量级的对象关系映射(ORM) 如果你需要在android中使用ORMLite 你需要进入官方网站http://ormlite.com/ 中下载 下载了这两个包以后,你还需要在对应的 ...
- 关于IOS中UIWebView 加载HTML内容
NSString *strContent=[info objectForKey:@"newContent"]; { NSArray *paths = NSSearchPathFor ...
- android 读取用户号码,手机串号,SIM卡序列号
简介: IMSI:international mobiles subscriber identity国际移动用户号码标识,这个一般大家是不知道,GSM必须写在卡内相关文件中:MSISDN:mobile ...
- UVa465 - Overflow
题目地址:点击打开链接 C++代码: #include <cstdlib> #include <cstdio> int main() { char s1[10000],s2[1 ...
- Spring.NET学习笔记
http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html