ASP. NET implements garbled processing of Chinese file names for the download function

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,
Add the following code:
...
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
Response. AppendHeader ("Content-Disposition", "attachment; filename =" "+ Server. UrlEncode (fileName) + """);
You can.
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: 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.