Asp. Net intermediate-5. HttpHandler case

Source: Internet
Author: User
HttpHandler1

(1) HttpHandler is a response to a request. It can output common html content, images, or a file (download)

(2) Output a dynamically created image (you can understand it)

(3) Case 1: Show visitor information in the image


(4) Case 2: enter a friend's name to generate a spoof image Link

(5) The registration and logon verification codes displayed on the Internet are also dynamically generated images and 55. la is also implemented in this way.

 

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 ("filter word .txt"); Response. addHeader ("Content-Disposition", string. format ("attachment; filename = \" {0} \ "", encodeFileName); where filename is the encoded file name. The filename segment is the recommended file name to save.

Example:

(1) Step 1

Xiazai1.aspx
<Form id = "form1" runat = "server"> <div> <a href = "2013-05-07 21-05-25.png"> image 1 </a> </div> </form>

 

(2) Step 2

Xiazai1.aspx change

<Form id = "form1" runat = "server"> <div> <a href = "2013-05-07 21-05-25.png"> image 1 </a>

<A href = "./xiazai2.ashx"> Image 2 </a>

        </div>    </form>

Xiazai2.ashx

public void ProcessRequest(HttpContext context)    {        context.Response.ContentType = "image/jpeg";        context.Response.AddHeader("Content-Disposition", "attachment;filename=image2.jpg");        context.Response.WriteFile("2013-05-07 21-05-25.png");    }

 

(3) Step 3

If it is saved as a Chinese name, garbled characters will appear.

Public void ProcessRequest (HttpContext context) {context. response. contentType = "image/jpeg"; context. response. addHeader ("Content-Disposition", "attachment?filename=brains .jpg"); context. response. writeFile ("2013-05-07 21-05-25.png ");}

NeedBrainchild .jpgAddUrlEncOde

Public void ProcessRequest (HttpContext context) {context. response. contentType = "image/jpeg"; string filename = HttpUtility. urlEncode ("brainchild .jpg"); context. response. addHeader ("Content-Disposition", "attachment; filename =" + filename); context. response. writeFile ("2013-05-07 21-05-25.png ");}

 

(2) Use of dynamic output. You do not need to save the resources to the disk and then output the resources (there is no problem of duplicate file names, and the files are not generated on the server ). Case: click the link to bring up the image download dialog box. Web principles: do not generate temporary files if you can directly output the generated content to the browser in the form of a stream.

(3) Use npoito generate an excelable table to show the dialog box for the user to download. The file name is ...xls ".

(4) exercise: export data from the database user table to an Excel file for users to download. Mdf is placed under App_Data. asp. Net does not use the code that sets DataDirectory and reads data using DataReader.

(5) exercise: Add a level field to the user table. Only the logged-on user can download the image file under images (which indicates whether to log on in the Session). If the user does not log on, first redirect to the logon interface for the user to log on, if the user logs on successfully, the download list page is displayed, and the download link is fixed. If the login user is a normal user, add "free user trial" in the upper left corner of the image. "Exit safely ". Draw the page flow.

(6) exercise: add the logon verification code to the above program.

 

(Not carefully read. If you want to see more details, go and have a look)

[Chuan Zhi podcast. Net training -asp.net intermediate] 8HttpHandler exercise description. avi

~

~

~

[Chuan Zhi podcast. Net training -asp.net intermediate] 13 case: Library permission control 4.avi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.