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.