A few days ago, the download function of file management was added to the system. It is required to be implemented in the ASPX file to control permissions.
So add the following code:
...
Response.ContentType = mime; //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 defeats my original intention of setting the default file name.
Solution 1: URL-encode fileName and change the underlined sentence to
Response.AppendHeader("Content-Disposition", "attachment; filename="" + Server.UrlEncode(fileName) + """);
That’s it.
Although the problem of Chinese garbled characters has been solved, there is still another problem: during testing, the name of the downloaded and saved file sometimes becomes the name of the page (.aspx). Although the content can be downloaded locally, the file name and type need to be changed. , this will cause great confusion to users.
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 and the fileName is "Download.pdf", we only need to change the download URL to http://localhost/download.aspx/download . pdf?id=123 However, the underlined code above can be commented out. Try it, you will be very satisfied with the result!
http://www.cnblogs.com/niit007/archive/2006/08/13/ 475561.html