我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

ASP.NET 高级编程基础第十篇—HttpHandler处理

前言:好几天没有写博客了,今天继续写我们的asp.net高级编程基础系列的第十篇,Httphandler处理,通过这篇博客我们可以了解到Httphandler是个干什么的了,当然本来想写写这个底层的,但是想想我们可以自己查一下,所以就没有写了,希望我们能够共同进步。

  1. HttpHandler处理

(1) HttpHandler是对请求的响应,可以输出普通的Html内容,也可以输出图片,也可以输出一个文件。

(2) 案例1:图片中显示访问者的信息

新建一个ashx项目,起名为访问者信息.ashx项目,在里面写入如下代码:

 1      context.Response.ContentType = "Image/JPEG";
 2 
 3      using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(300, 300))
 4 
 5      {
 6 
 7           using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
 8 
 9           {
10 
11               g.DrawString("IP地址是:" + context.Request.UserHostAddress, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 0, 0);
12 
13               g.DrawString("操作系统:" + context.Request.Browser.Platform, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 0, 50);
14 
15               g.DrawString("浏览器信息:" + context.Request.Browser.Type, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 0, 100);
16 
17            }
18 
19     bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
20 
21        }

 

(3) 案例2:填入朋友的名字,就能够生成要恶搞的图片连接

1)新建一个一般处理程序.ashx,在其中写入如下代码,验证的话在浏览器输入参数。

   

 1      context.Response.ContentType = "Image/JPEG";
 2 
 3         string name = context.Request["Name"];
 4 
 5         string fullPath = HttpContext.Current.Server.MapPath("刘亦菲.jpg");
 6 
 7         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullPath))
 8 
 9         {
10 
11             using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
12 
13             {
14 
15                 g.DrawString(name, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 113, 35);
16 
17             }
18 
19             bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
20 
21         }

 

  1. HttpHandler实现文件下载

(1) 如果HttpHandler输出的是HTML,txt,jpeg等类型的信息,那么浏览器会直接显示,如果希望弹出保存对话框,则需要添加

Header:string encodeFileName=HttpUtility.UrlEncode(“过滤词.txt”);

Response.AddHeader(”content-Disposition”,string.Format(”attachment;filename=\”{0}\””,encodeFileName));其中filename后面为编码后的文件名,filename段为建议的保存文件名。

(2) 1) 新建一个下载.html页面,写入如下代码:

   <a href="刘亦菲.jpg">图片1</a>

 2) 新建一个1.txt文件,乱写一些东西,在下载页面中写入如下代码:

   <a href="1.txt">txt下载</a>

 3) 新建一个一般处理程序下载.ashx

        context.Response.ContentType = "Image/JPEG";

        context.Response.AddHeader("content-Disposition", "attachment:filename=刘亦菲.jpg");

        context.Response.WriteFile("刘亦菲.jpg");

 4) 在下载.htm中写入如下代码

<a href="下载.ashx">下载</a>

(3) 动态输出用处,不要再把资源保存到磁盘上面在输出(不会有文件重名的问题,文件不生成在服务器端)。案例:点击链接弹出图片下载对话框,web原则,能直接将生成的内容以流的形式输出给浏览器,就不要生成临时文件夹,

(4) 用NPOI动态生成一个Excel表然后弹出对话框让用户下载,文件名是:”用户列表.xls”。

 1) 新建一个asp.net web应用程序起名为导出excel.aspx,新建一个BLL文件夹,下载NPOI的所有dll文件加到文件夹下面,添加dll引用。

 <a href="DownLoadExcel.ashx">下载Excel</a>

2) 新建一个一般处理程序DownLoadExcel.ashx,在里面写入如下代码:

  

 1  context.Response.ContentType = "application/x-excel";
 2 
 3    string filename = HttpUtility.UrlEncode("动态数据.xls");
 4 
 5    context.Response.AddHeader("content-Disposition", "attachment:filename=" + filename);
 6 
 7    HSSFWorkbook workbook = new HSSFWorkbook();
 8 
 9    HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
10 
11    HSSFRow row = (HSSFRow)sheet.CreateRow(0);
12 
13    HSSFCell cell = row.CreateCell(0, HSSFCell.CELL.TYPE.STRING);
14 
15    row.setCellValue("Hello");
16 
17    row.CreateCell(0, HSSFCell.CELL.TYPE.STRING).SetCellValue(3.14);
18 
19    workbook.Write(context.Response.OutputStream);

 

  1. ASP.NET补充问题

(1) WebApplication每次修改以后点击[生成解决方案],也能立即看到修改结果,不需要重新启动浏览器。原理:生成以后才将变化的部分生成dll,而webSite则是每次访问页面的时候会检查cs文件是否变化了,变化了则重新自动编译,所以每次修改以后都会立即有效果的。

(2) 方便开发不用每次调试都设定起始页,在项目中的选项中设定[web]—>启动操作—>当前页面—>这样当前激活的页面就是起始页。

posted @ 2012-08-22 10:44  Kencery  阅读(2723)  评论(2编辑  收藏  举报
友情链接:初心商城