Asp. NET to implement the download function of Chinese file name garbled processing
Source: Internet
Author: User
A few days earlier add file management downloads to the system, requiring controls that are implemented in the ASPX file to perform permissions,
Then add the following code:
...
Response.ContentType = MIME; The corresponding MIME TYPE
Response.appendheader ("Content-disposition", "attachment; Filename= "" "+filename +" "" ");
Response.BinaryWrite (bytes);
Response.End ();
...
When the filename contains Chinese, when the file is downloaded, the filename becomes garbled and needs to be modified by the user, which also violates the original intention of setting the preset filename.
Solution 1: URL code for filename, the underlined sentence to be changed to
Response.appendheader ("Content-disposition", "attachment; Filename= "" + Server.URLEncode (fileName) + "");
Will be.
Although the Chinese garbled problem is solved, but there is also a problem: in the test, download the saved file name will sometimes become the name of the page (. aspx), although the content can be downloaded to the local, but need to change the file name and type, this will cause great confusion to users.
There is also a way to solve the above two problems very simply:
Workaround 2: Assuming the current URL is http://localhost/download.aspx?id=123, filename is "download. pdf", we simply change the download URL to http://localhost/ download.aspx/download. Pdf?id=123 can be, the above underlined that the code can be commented out, try, the results must make you very satisfied!
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.