Modify the HTTP header to change the display image to download

Source: Internet
Author: User

Using system; using system. collections. generic; using system. LINQ; using system. web; namespace httphandler {// <summary> // Summary of xiazai1 /// </Summary> public class xiazai1: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "image/JPEG"; // context. response. addheader ("content-disposition", "attachment: filename=haha.jpg"); // you can use the following Asp.net method to add a message header. addheader is an old ASP-compatible method context. response. appendheader ("content-disposition", "attachment: filename=hahaha.jpg"); context. response. writefile ("content/dsc06942.jpg");} public bool isreusable {get {return false ;}}}}

What is the difference between the results and the Web server? Or a browser?

After checking the information, it turns out that a wrong character is entered. You should keep up with the semicolon after the attachment, not the colon!

If you want to change hahaha.jpg to Chinese, you need to encode the Chinese, such

Public void processrequest (httpcontext context) {context. response. contenttype = "image/JPEG"; // context. response. addheader ("content-disposition", "attachment: filename=haha.jpg"); // You can also add a message header using the following Asp.net method, addheader is an old ASP-compatible method string filename = httputility. urlencode ("haha.jpg"); context. response. addheader ("content-disposition", "attachment; filename =" + filename); context. response. writefile ("content/dsc06942.jpg ");}

In this way, you can change the name of an image or other file on the server for the user to download! Everything is under control

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.