HttpHandler: Processing request information and sending a response (Response).
HttpModule: Writes text to the HTTP request output stream via HTTP module, HttpModule executes first
The difference between them two:
In the process of processing, the page handler is going through the processing of Httpmodule,httphandler HttpModule for the processing of some events before and after the page processing, HttpHandler the actual page processing.
HttpModule will process the page before and after the page is processed, so it will not affect the actual page request. It is usually used to add some information (such as a copyright notice) to the head or tail of each page.
IHttpModule: is a size-all type that calls to it regardless of what file the client requests, such as Aspx,rar,html's request.
IHttpHandler: It is a picky type, and only the file types that are registered by ASP (for example, Aspx,asmx, and so on) will be called.
Case: Print the specified text at the location specified in the picture (ASHX program below)
Context. Response.ContentType = "Image/jpeg"; string name = Context. request["Name"]; The request parameter of the URL is string fullpath = HttpContext.Current.Server.MapPath ("20110410231802.jpg"); Specifies the picture path 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 ("Black Body", System.Drawing.Brushes.Pink, 175,); Set parameter } bitmap. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg); }
Case: Httphander implementation file download
If the Httphander output is html,txt,jpeg and other types of information, then the browser will be displayed directly, if you want to pop up the Save dialog box
Need header add: content-disposition:attachment;filename= custom. jpg
Context. Response.ContentType = "Image/jpeg"; String name = Httputility.urlencode ("haha. jpg"); URL encoding to prevent garbled context. Response.AddHeader ("Content-disposition", "attachment;filename=" + name); Add hander context. Response.WriteFile ("20110410231802.jpg"); Output file
Then call <a href= "DOWN.ASXH" in the HTML page > picture Download </a>
Case: Click on the image button for download
File download 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"); Solve the Chinese garbled response.addheader ("content-disposition", "attachment; Filename= "+ server.urlencode (file. Name)); Solve the Chinese file name garbled Response.AddHeader ("Content-length", file. Length.tostring ()); Response.ContentType = "Appliction/octet-stream"; Response.WriteFile (file. FullName); Response.End (); }}
Another similar way to implement a download
Implement file Download String filePath = soft.rows[0]["Appadd"]. ToString (); string fileName = soft.rows[0]["AppName"]. ToString (); FileInfo info = new FileInfo (Server.MapPath (FilePath)), long fileSize = info. Length; Response.Clear (); response.contentencoding = encoding.getencoding ("UTF-8"); Solve Chinese garbled Response.ContentType = "application/x-zip-compressed"; Response.AddHeader ("Content-disposition", "attachment;filename=" + server.urlencode (filename + info). Extension));
WebClient Download in streaming mode
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 and HttpModule and implementation file download