Garbled characters for uploading/downloading Chinese file names in different browsers

Source: Internet
Author: User

The encoding format that the browser can correctly recognize. As long as the corresponding Content-Disposition is set according to this encoding, there should be no garbled Chinese file names.
First, the Content-Disposition value can have the following encoding formats:
1. urlencode:

Content-Disposition: attachment; filename = "struts2.0% E4 % B8 % AD % E6 % 96% E6 % 87% E7 % A8 % 8B. chm"

2. Base64 encoding:

Content-Disposition: attachment; filename = "=? UTF8? B? C3RydXRzMi4w5Lit5paH5pWZ56iLLmNobQ =? ="

3. RFC2231 standards:

Content-Disposition: attachment; filename * = UTF-8 ''% E5 % 9B % 9E % E6 % 89% A7.msg

4. File name for direct ISO encoding:

Content-Disposition: attachment; filename = "test .txt"

The corresponding encoding formats supported by browsers are as follows:

1. IE browser, which uses URLEncoder Encoding
2. Opera browser, using filename *
3. Chinese output using ISO encoding in Safari
4. The Chrome browser uses Base64 encoding or ISO encoding for Chinese output.
5. the FireFox browser uses Base64, filename *, or ISO-Encoded chinese output.


New_filename = URLEncoder. encode (filename, "UTF8 ");
// If no UA is available, IE is used for encoding by default, because IE is still the dominant
Rtn = "filename = \" "+ new_filename + "\"";
If (userAgent! = Null)
{
UserAgent = userAgent. toLowerCase ();
// IE browser, which can only be subject to URLEncoder Encoding
If (userAgent. indexOf ("msie ")! =-1)
{
Rtn = "filename = \" "+ new_filename + "\"";
}
// Operabrowser can only use filename *
Else if (userAgent. indexOf ("opera ")! =-1)
{
Rtn = "filename * = UTF-8'' "+ new_filename;
}
// Safari, which can only be output in ISO format
Else if (userAgent. indexOf ("safari ")! =-1)
{
Rtn = "filename = \" "+ new String (filename. getBytes (" UTF-8 ")," ISO8859-1 ") + "\"";
}
// The Chrome browser can only use MimeUtility encoding or ISO encoding for Chinese output
Else if (userAgent. indexOf ("applewebkit ")! =-1)
{
New_filename = MimeUtility. encodeText (filename, "UTF8", "B ");
Rtn = "filename = \" "+ new_filename + "\"";
}
// In FireFox, you can use MimeUtility, filename *, or ISO-Encoded chinese output.
Else if (userAgent. indexOf ("mozilla ")! =-1)
{
Rtn = "filename * = UTF-8'' "+ new_filename;
}
}

Currently, I have tested that Chinese file names can be entered in several browsers.
However, there may be garbled characters in different browser versions .....
Author: xrzs


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.