ASP. NET implements garbled processing of downloaded Chinese file names

Source: Internet
Author: User
A few days ago, we added the file management download function to the system, which must be implemented in the aspx file for permission control, so we added the followingCode:
Response. contenttype = mime; // the corresponding MIME type
Response. appendheader ("content-disposition", "attachment; filename = \" "+ filename + "\"");

Response. binarywrite (bytes );
Response. End ();

When filename contains Chinese characters, when the file is downloaded and saved, the file name becomes garbled and needs to be modified by the user, which violates the original intention of setting the preset file name.

Solution 1: perform URL encoding on filename and change the underlined sentence to response. appendheader ("content-disposition", "attachment; filename = \" "+ server. urlencode (filename) +.

Although the Chinese Garbled text problem has been solved, there is another problem: During the test, the downloaded and saved file names sometimes become the name of the page (. aspx), although the content can be downloaded locally, but you need to change the file name and type, this will bring a lot of confusion to the user.

There is another way to easily solve the above two problems:

Solution 2: assume that the current URL is http: // localhost/download. aspx? Id = 123, filename is "Download objects", we just need to change the download URL to http: // localhost/download. aspx/download objects? Id = 123. The underlined code can be commented out. Try it and you will be very satisfied with the result!
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: 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.