SoFunction
Updated on 2025-03-07

.net download file with garbled Chinese name from the server solution

Download the solution for garbled Chinese name from the server. There are not many specific text instructions, so I will post the code directly. The specific code is as follows:

try
    {
      string excelName = Request["excelName"].ToString(); //File path      string fileName = Request["fileName"].ToString(); //Downloaded file name      if(!((excelName)|| (fileName)))
      {
         = "GB2312";
         = ("GB2312");
        ="application/-excel";
        // Firefox browser does not need to convert Chinese file names into encoding format        if (["http_user_agent"].ToLower().IndexOf("firefox") == -1)
        {
          excelName = (excelName, .UTF8);
        }
        ("Content-Disposition", "attachment;filename=" +excelName);
        (fileName);
        ();
        ();
      }
    }
    catch
    { }

 Pay attention to the browser. IE needs to convert the Chinese file name encoding format, but the firefox browser does not use it, otherwise the downloaded Chinese file name will be garbled.

()Download Chinese file name garbled problem

First, show my problem-solving code:

 ("Content-Disposition", 
 "attachment; filename=" + (fileName, "UTF-8"));  

1. HTTP message header

(1) General information header

It can be used in request messages or in response information, but has nothing to do with the transmitted entity content, such as Data and Pragma

Main: Cache-Control, Connection, Data, Pragma, Trailer, Transfer-Encoding, Upgrade

(2) Request header

It is used to pass additional information to the server in the request message, mainly including the data type that the client can accept, compression method, language, as well as the information retained on the client computer and the hyperlink source address that issued the request.

Main: Accept, Accept-Encoding, Accept-Language, Host,

(3) Response header

Used to pass additional information to the client in response messages, including the name of the service program, the method of requiring the client to perform authentication, the requested resource has been moved to the new address, etc.

Main: Location, Server, WWW-Authenticate (authentication header)

(4) Entity head

Meta-information used as entity content describes the attributes of entity content, including the type, length, compression method, the time of the last modification and the validity period of the data, etc.

Main: Content-Encoding, Content-Language, Content-Length, Content-Location, Content-Type

(4) Expansion header

Main: Refresh, Content-Disposition

 2. The role of several main heads

(1) The role of Content-Type

The purpose of this entity header is to let the server tell the browser what file type the data it sends belongs to.

For example: When the value of Content-Type is set to text/html and text/plain, the former will allow the browser to parse the received entity content in HTML format, and the latter will allow the browser to parse it in plain text.

(2) The role of Content-Disposition

When the type of Content-Type is the type to be downloaded, this information header will tell the browser the name and type of the file.

When explaining this content, Teacher Zhang also explained the solution to the garbled Chinese file name. He usually thought of using getBytes(). In fact, the attachment name encoding method of email should be used to encode the file name, but IE does not support this method (supported by other browsers), and uses the .* package ("Chinese.txt") method for encoding.

Example of Content-Disposition extension header:

 <%@ page pageEncoding="GBK" contentType="text/html;charset=utf-8" import=".*,.*" %> 
 <%=(, , ).format(new Date()) 

%> 
<%         
("Content-Type","video/x-msvideo");  
("Content-Disposition", "attachment;filename="); 
%> 

The type specified in Content-Disposition is the file extension, and the file type picture in the pop-up download dialog box is displayed according to the file extension. After clicking save, the file is named with the value of filename, and the save type shall prevail.

Note: Before setting the Content-Disposition header field, be sure to set the Content-Type header field.

(3) The role of the Authorization header The role of Authorization is that when the client access is password protected, the server will send a 401 status code and a WWW-Authenticate response header, requiring the client to use Authorization to answer.

For example:

<%@ page pageEncoding="GBK" contentType="text/html;charset=utf-8" import=".*,.*" %>  
<%=(, , ).format(new Date()) 
%> 
<%  (401); 
("WWW-Authenticate", "Basic realm=\"Tomcat Manager Application\""); 
%> 

3. How to implement file downloading. To implement file download, we only need to set two special corresponding headers. What are their headers? If the file name is in Chinese, how to solve it?

Two special corresponding headers:

----Content-Type:    
application/octet-stream ----Content-Disposition: attachment;filename= 

For example:

("image/jpeg");("Content- Disposition","attachment;filename="); 

If there is Chinese in the filename parameter in the file, garbled code will appear.

Solution:

(1)("Chinese.txt");//The current version of IE is not good yet

(2) new String("Chinese".getBytes("GB2312"),"ISO8859- 1");//In fact, this is wrong

4. Test and analyze the problem of garbled file names

()Download Chinese file name garbled problem

("Content-Disposition", "attachment; filename=" + (fileName, "UTF-8")); 

The downloaded program has the above sentence. Generally, the file name will be displayed correctly on the IE6 download prompt box, whether in Simplified Chinese or Japanese.
However, at that time, I did not carefully test the case that the file name was a very long Chinese file name. Now after careful testing, I found that the text only has more than 17 words and cannot be downloaded.

The analysis is as follows:

1. Through the original method, that is, first use URLEncoder to encode. When there are more than 17 Chinese texts, IE6 cannot download the file. This is a bug in IE, see Microsoft's knowledge base article KB816868.

The reason may be that when IE processes the Response Header, the length of the header is limited to about 150 bytes. If a Chinese character is encoded into UTF-8, it is 9 bytes, then 17 characters are 153 bytes, so an error will be reported. And it is not right not to follow the suffix.

2. Solution: Encoding the file name into ISO8859-1 is an effective solution.

The code is as follows:

( "Content-Disposition", "attachment;filename=" + new String( ("gb2312"), "ISO8859-1" ) ); 

When ensuring that the attachment file names are all simplified Chinese, this method is indeed the most effective, and there is no need to let customers upgrade IE one by one. If * compatriots use it, just change gb2312 to big5. However, today's systems usually have international support, and UTF-8 is widely used.

If the file name contains simplified Chinese, traditional Chinese, and Japanese.

Then garbled codes are created. In addition, downloading on Firefox (v1.0-en) is also garbled.

3. See the form of the Chinese attachment name in the email, use outlook to create a new email with Chinese attachment, and then look at the source code of the email and find:

Content-Disposition: attachment;  filename="=?gb2312?B?0MK9qCDOxLG+zsS1tS50eHQ=?="

In principle, you can display Chinese name attachments using this filename, but now IE does not support it, Firefox is supported. Try to use the javamail method to encode the file name, that is, encode it into the form =?gb2312?B?xxxxxxxx?=, and find the corresponding standard support from RFC1522. Consider the compromise, combine one or two methods,

The code snippet is as follows:

String fileName = ((), "UTF-8");  /* 
* see /?kbid=816868  */  
if (() > 150) {   String guessCharset = xxxx   //Get possible encoding based on the locale of the request,

The Chinese operating system is usually gb2312

fileName = new String(().getBytes(guessCharset), "ISO8859-1");  }
  ("Content-Disposition", "attachment; filename=" + fileName); 

The principle of encoding conversion:

First, set the encoding to GB2312 character encoding in the source program, and then convert the source program into byte code according to Unicode encoding and load it into memory (the byte codes loaded into memory by Java are Unicode encoding), and then use GB2312 encoding to obtain the byte array of Chinese strings, and then generate a Unicode string in the form of ISO8859-1 encoding (at this time, the 4 bytes become 8 bytes, and the high-digit bytes are zero-complemented),

Java Training

Beijing java training

Java training class
Java employment training
Java training institution
Software training
The best java training

When transmitted on the network, because the characters in the setHeader method can only be transmitted according to ISO8859-1, the Unicode characters are converted into ISO8859-1 encoding and transmitted to the browser (that is, all the zeros that were supplemented just now are removed). At this time, the characters in ISO8859-1 code received by the browser can display Chinese because they comply with the GB2312 encoding.

5. Coding problems when translating jsp into class

Code block 1 in Notepad:

 &lt;%=    
 "a" in Chinese.length() 
 %&gt;  

Code Block 2:

 &lt;%@ page pageEncoding="gbk"%&gt; 
 &lt;%=     
 "a" in Chinese.length() 
 %&gt; 

Why is the output value above 5, and the output value below 3?

Because the above code does not add the encoding instructions for the file, when the WEB application translates jsp into a class file, the content of the string is calculated according to the encoding ASCII code specified by the default saving method. In UTF-8, the original ASCII character occupies one byte and the Chinese character occupies two bytes, corresponding to two characters, and the length becomes 5. The following is the GBK encoding, one Chinese character and one English correspond to one character, and the result is 3.

(...) When there are spaces in the file name

 String fileName = (());  
 String formatFileName = encodingFileName(name);//Define the method encodingFileName(String fileName); ("Content-Disposition", "attachment; filename=" + formatFileName ); 
// Handle the spaces in the file name// where %20 is the encoding of space under UTF-8public static String encodingFileName(String fileName)
{    
String returnFileName = "";    
try {       
returnFileName = (fileName, "UTF-8");      
returnFileName = (returnFileName, "+", "%20");       
if (() &gt; 150) {         

returnFileName = new String(("GB2312"), "ISO8859-1");  
returnFileName = (returnFileName, " ", "%20");     
}      } catch (UnsupportedEncodingException e) {       
();        
if (()) {       
("Don't support this encoding ...");   
}      }     
return returnFileName;  
}  

The above is the entire content of the garbled solution for downloading the file in Chinese name from the server. I hope you like it.