Example of garbled file names downloaded from various browsers

Source: Internet
Author: User

From: http://hi.baidu.com/nathena/blog/item/d9b169fd422cc71e09244dd9.html

 

The downloaded file name is garbled
Scenario 1
1. GBK File
2. The downloaded file name does not contain spaces.
The following code
<? PHP
// GBK charset
$ Filename = "GBK file. The file name does not contain spaces !. TXT ";
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Echo "GBK file, the file name does not contain spaces! ";
?>
Test Results
1. IE is normal.
2. FF is normal.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
Scenario 2
1. GBK File
2. The downloaded file name contains spaces.
The following code
<? PHP
// GBK charset
$ Filename = "GBK file. The file name contains a blank space !. TXT ";
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Echo "GBK file, file name contains space! ";
?>
Test Results
1. IE is normal.
2. FF only contains the first part of the space.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
Scenario 3
1. utf8 File
2. The downloaded file name does not contain spaces.
<? PHP
// Utf8 charset
$ Filename = "utf8 file. The file name does not contain spaces !. TXT ";
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Echo "utf8 file, the file name does not contain spaces! ";
?>
Test Results
1. ie garbled.
2. FF is normal.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
Scenario 4
1. utf8 File
2. The downloaded file name contains spaces.
<? PHP
// Utf8 charset
$ Filename = "utf8 file. The file name contains an empty space !. TXT ";
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Echo "utf8 file, the file name contains spaces! ";
?>
Test Results
1. ie garbled.
2. FF only contains the first part of the space.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
Scenario 5
According to rfc2231, the content-Disposition of multi-language encoding should be defined as follows:
Content-Disposition: attachment; filename * = utf8 'browser language' URL Encoding
1. GBK/utf8 File
2. The downloaded file name contains spaces.
3. attachment name urlencode
<? PHP
$ Filename = "GBK file. The file name contains spaces !. TXT ";
$ Encoded_filename = urlencode ($ filename );
$ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename * = GBK/'zh _ Cn/'. $ encoded_filename );
Echo "GBK file, file name contains space ";
?>
<? PHP
$ Filename = "utf8 file. The file name contains spaces !. TXT ";
$ Encoded_filename = urlencode ($ filename );
$ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename * = utf8/'zh _ Cn/'. $ encoded_filename );
Echo "utf8 file, file name contains space ";
?>
Test Results
1. IE is abnormal.
2. FF is normal.
3. Safari garbled.
4. Opera is normal.
5. Chrome is abnormal.
<? PHP
$ Filename = "utf8 file. The file name contains spaces !. TXT ";
$ Encoded_filename = urlencode ($ filename );
$ Encoded_filename = str_replace ("+", "% 20", $ encoded_filename );
Header ('content-type: Application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ encoded_filename );
Echo "utf8 file, file name contains space ";
?>
Test Results
1. ie garbled.
2. FF shows the URL encoding.
3. Safari displays URL encoding.
4. Opera displays URL encoding.
5. Chrome is normal.

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<%
String filename = "utf8 file. The file name contains spaces !. TXT ";
Response. setcontenttype ("application/octet-stream ");
Response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes (), "ISO-8859-1 "));
Out. println ("utf8 file, file name contains space !. TXT "));
Out. Flush ();
Out. Close ();
%>
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String filename = "GBK file. The file name contains spaces !. TXT ";
Response. setcontenttype ("application/octet-stream ");
Response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes (), "ISO-8859-1 "));
Out. println ("GBK file, file name contains space !. TXT "));
Out. Flush ();
Out. Close ();
%>
Test Results
1. IE is normal.
2. FF only contains the first part of the space.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<%
String filename = "UTF file. The file name contains spaces !. TXT ";
Response. setcontenttype ("application/X-msdownload ");
Response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes ("UTF-8"), "ISO-8859-1 "));
Out. println (UTF file, file name contains space );
Out. Flush ();
Out. Close ();
%>
1. ie garbled.
2. FF only contains the first part of the space.
3. Safari garbled.
4. Opera is normal.
5. Chrome is normal.
Note !!! Safari keeps garbled.

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.