Web applications that want to force a resource to be downloaded rather than directly rendered in a Web browser issue a Content-Disposition
header in the HTTP response of the form:
Content-Disposition: attachment; filename=FILENAME
The filename
parameter can be used to suggest a name for the file into which the resource is downloaded by the browser. RFC 2183 (Content-Disposition), however, states in section 2.3 (The Filename Parameter) that the file name can only use US-ASCII characters:
Current [RFC 2045] grammar restricts parameter values (and hence Content-Disposition filenames) to US-ASCII. We recognize the great desirability of allowing arbitrary character sets in filenames, but it is beyond the scope of this document to define the necessary mechanisms.
There is empirical evidence, nevertheless, that most popular Web browsers today seem to permit non-US-ASCII characters yet (for the lack of a standard) disagree on the encoding scheme and character set specification of the file name. Question is then, what are the various schemes and encodings employed by the popular browsers if the file name “naïvefile” (without quotes and where the third letter is U+00EF) needed to be encoded into the Content-Disposition header?
For the purpose of this question, popular browsers being:
- Firefox
- Internet Explorer
- Safari
- Google Chrome
- Opera
I found out solution, that works for all my browsers (ie. all browsers I have installed - IE8, FF16, Opera 12, Chrome 22).
My solution is described in other thread: Java servlet download filename special characters
My solution is based on the fact, how browsers trying to read value from
filename
parameter. If there is no charset specified in thefilename
parameter (for examplefilename*=utf-8''test.xml
) browsers expect that value is encoded in browser's native encoding.Different browsers expect diffrent native encoding. Usually browser's native encoding is utf-8 (FireFox, Opera, Chrome). But IE's native encoding is Win-1250. (I don't know anything about other browsers.)
Hence, if we put value into
filename
parametr, that is encoded by utf-8/win-1250 according to user's browser, it should work. At least, it works for me.In short, if we have file named
omáčka.xml
,for FireFox, Opera and Chrome I response this header (encoded in utf-8):
and for IE I response this header (encoded in win-1250):
Java example is in my post that is mentioned above.
I use the following code snippets for encoding (assuming fileName contains the filename and extension of the file, i.e.: test.txt):
PHP:
Java:
I ended up with the following code in my "download.php" script (based on this blogpost and these test cases).
This uses the standard way of filename="..." as long as there are only iso-latin1 and "safe" characters used; if not, it adds the filename*=UTF-8'' url-encoded way. According to this specific test case, it should work from MSIE9 up, and on recent FF, Chrome, Safari; on lower MSIE version, it should offer filename containing the ISO8859-1 version of the filename, with underscores on characters not in this encoding.
Final note: the max. size for each header field is 8190 bytes on apache. UTF-8 can be up to four bytes per character; after rawurlencode, it is x3 = 12 bytes per one character. Pretty inefficient, but it should still be theoretically possible to have more than 600 "smiles" %F0%9F%98%81 in the filename.
Classic ASP Solution
Most modern browsers support passing the
Filename
asUTF-8
now but as was the case with a File Upload solution I use that was based on FreeASPUpload.Net (site no longer exists, link points to archive.org) it wouldn't work as the parsing of the binary relied on reading single byte ASCII encoded strings, which worked fine when you passed UTF-8 encoded data until you get to characters ASCII doesn't support.However I was able to find a solution to get the code to read and parse the binary as UTF-8.
Credit goes to Pure ASP File Upload by implementing the
BytesToString()
function frominclude_aspuploader.asp
in my own code I was able to getUTF-8
filenames working.Useful Links
Multipart/form-data and UTF-8 in a ASP Classic application
Unicode, UTF, ASCII, ANSI format differences
There is no interoperable way to encode non-ASCII names in
Content-Disposition
. Browser compatibility is a mess.The theoretically correct syntax for use of UTF-8 in
Content-Disposition
is very weird:filename*=UTF-8''foo%c3%a4
(yes, that's an asterisk, and no quotes except an empty single quote in the middle)This header is kinda-not-quite-standard (HTTP/1.1 spec acknowledges its existence, but doesn't require clients to support it).
There is a simple and very robust alternative: use a URL that contains the filename you want.
When the name after the last slash is the one you want, you don't need any extra headers!
This trick works:
And if your server supports URL rewriting (e.g.
mod_rewrite
in Apache) then you can fully hide the script part.Characters in URLs should be in UTF-8, urlencoded byte-by-byte: