I haven't written a blog for several days. Today I will continue to write Part 10 of our asp.net advanced programming basic series, Httphandler processing. Through this blog, I can see what Httphandler is doing, of course we wanted to write this underlying layer, but we can check it by ourselves, so we didn't write it. We hope we can make common progress.
- HttpHandler Processing
(1) HttpHandler is a response to a request. It can output common Html content, images, and files.
(2) Case 1: Show visitor information in the image
Create an ashx project named "Visitor Information. ashx project" and write the following code in it:
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 11g. drawString ("IP Address:" + context. request. userHostAddress, new System. drawing. font ("", 20), System. drawing. brushes. red, 0, 0); 12 13g. drawString ("Operating System:" + context. request. browser. platform, new System. drawing. font ("", 20), System. drawing. brushes. red, 0, 50); 14 15 GB. drawString ("browser information:" + 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) Case 2: enter a friend's name to generate an image connection to spoof.
1) create a general processing program. ashx, write the following code in it, and enter parameters in the browser for verification.
1 context. response. contentType = "Image/JPEG"; 2 3 string name = context. request ["Name"]; 4 5 string fullPath = HttpContext. current. server. mapPath ("Liu Yifei .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 15g. 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}
- HttpHandler implements File Download
(1) If HttpHandler outputs HTML, txt, jpeg, and other types of information, the browser will directly display the information. If you want to pop up the Save dialog box, you need to add
Header: string encodefilename=httputility.urlencode(.txt ");
Response. addHeader ("content-Disposition", string. format ("attachment; filename = \" {0} \ ", encodeFileName); where filename is followed by the encoded file name, And the filename segment is the recommended save file name.
(2) 1) Create a New. html page and write the following code:
<A href = "Liu Yifei .jpg"> image 1 </a>
2) create a new 1.txt file, write something in disorder, and write the following code on the download page:
<A href = "1.txt"> txt download </a>
3) create a general processing program to download. ashx
Context. Response. ContentType = "Image/JPEG ";
Context. Response. AddHeader ("content-Disposition", "attachment: filename= .jpg ");
Context. Response. WriteFile ("Liu Yifei .jpg ");
4) write the following code in download .htm:
<A href = "Download. ashx"> download </a>
(3) dynamic output is useful. Do not store resources on the disk for output (there will be no problem of duplicate file names, and files will not be generated on the server side ). Case: click the link to bring up the image Download Dialog Box. The web principle is that the generated content can be directly output to the browser in the form of a stream, so that temporary folders are not generated,
(4) use NPOI to dynamically generate an Excel table and then bring up a dialog box for users to download. The file name is "" .xls ".
1) Create an asp.net web application named "Export excel. aspx", create a new BLL folder, download all dll files of NPOI to the folder, and add dll references.
<A href = "DownLoadExcel. ashx"> download Excel </a>
2) create a general processing program DownLoadExcel. ashx and write the following code in it:
1 context. response. contentType = "application/x-excel"; 2 3 string filename = HttpUtility. urlEncode ("Dynamic Data .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 );
- ASP. NET supplement
(1) After each WebApplication modification, click [generate solution] to view the Modification result immediately without restarting the browser. Principle: After a dll is generated, the changed part is generated, while the webSite checks whether the cs file is changed every time the page is accessed. If the file changes, it re-compiles automatically, therefore, each modification will immediately take effect.
(2) to facilitate development, you do not need to set the start page for each debugging. In the options of the project, set [web]-> Start Operation-> current page-> so that the currently activated page is the start page.