I am using HttpContext object implemented in HttpHandler child to download a file, when I have non-ascii characters in file name it looks weird in IE whereas it looks fine in Firefox.
below is the code:-
context.Response.ContentType = ".cs";
context.Response.AppendHeader("Content-Length", data.Length.ToString());
context.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}",filename));
context.Response.OutputStream.Write(data, 0, data.Length);
context.Response.Flush();
when I supply 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó' in file name field it looks different than what I have in file name it looks fine in firefox. adding EncodingType and charset has been of no use.
In ie it is 'ß''ä''ö''ü''ó''ß''ä''ö''ü'_'ó' and in firefox it is 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó'.
Any Idea how this can be fixed?
I`m using
Uri.EscapeUriString
for converts all characters to their hexadecimal representation, andstring.Normalize
for Unicode normalization form C. (tested in ASP.NET MVC5 framework 4.5)For me this solution is working on all major browsers:
Using ASP.NET MVC 3.
The Replace is necessary, because Chrome doesn't like Comma (,) in parameter values: http://www.gangarasa.com/lets-Do-GoodCode/tag/err_response_headers_multiple_content_disposition/
For Asp.Net Core (version 2 as of this post) UrlPathEncode is deprecated, here's how to achieve the desired result:
You may want to read RFC 6266 and look at the tests at http://greenbytes.de/tech/tc2231/.
HttpUtility.UrlPathEncode might be a better option. As URLEncode will replace spaces with '+' signs.
I had similar problem. You have to use HttpUtility.UrlEncode or Server.UrlEncode to encode filename. Also I remember firefox didn't need it. Moreoverit ruined filename when it's url-encoded. My code:
Edit
I have read specification more carefully. First of all RFC2183 states that:
But then I found references that [RFC 2045] is absolete and one must reference RFC 2231, which states:
Which means that you can use UrlEncode for non-ascii symbols, as long as you include the encoding as stated in the rfc. Here is an example:
Note that
filename
is included in addition tofilename*
for backwards compatibility. You can also choose another encoding and modify the parameter accordingly, but UTF-8 covers everything.