ASP.NET implements the Chinese file name of the download function

xiaoxiao2021-04-03  208

Add the download function of file management in the system earlier, requiring control of the control,

So add the following code:

...

Response.contentType =

Mime; // Corresponding MIME TYPE

Response.Appendheader ("Content-Disposition", "Attachment; FileName = /" " FileName " / ");

Response.binaryWrite

BYTES);

Response.end ();

...

When the file is included in the filename, the file name turns garbled when the file download is saved, and the user needs to be modified. This also violates the original intention of the preset file name.

Solution 1: Turn the URL encoding for FileName, change the sentence of underlined marking to

Response.Appendheader ("Content-Disposition", "Attachment; FileName = /" " Server.urlencode

FileName) "/" ");

Can you.

Although the Chinese garbled problem is solved, there is still a problem:

When testing, the downloaded file name sometimes turns into the name of this page (.aspx), although you can download the content to the local, but you need to change the file name and type, which will give users a big confusion.

There is also a way to solve the above two questions very simple:

Workaround 2: Assume that the current URL is

http://localhost/download.aspx? id = 123, filename is "Download .pdf", we only need to change the downloaded URL to

http://localhost/download.aspx/ Download .pdf? id = 123, the code that is marked above, you can comment, try, the result must make you very satisfied!

转载请注明原文地址:https://www.9cbs.com/read-131571.html

New Post(0)