Unicode in Content-Disposition Header

I use the HttpContext object implemented in the HttpHandler child file to download the file, when I have non-ascii characters in the file name, it looks weird in IE, while it looks great 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 put 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó' in the file name field, it looks different than I am in file name, it looks great in firefox. adding EncodingType and charset was useless.

The fact is that it is ß'Ãä'ÃÃöÃÃÃÃÃÃó³³ÃƒÃƒ¸ÃƒÃƒÃƒ¤ÃƒÃƒÃƒ¶ÃƒÃƒÃƒ¼ '_' ³Â³ ", and in firefox - ß '' ä '' ö '' ü '' ó '' ß '' ä '' ö '' ü '' ó '.

Any idea how this can be fixed?

+7
c # content-disposition
source share
5 answers

I had a similar problem. You must use HttpUtility.UrlEncode or Server.UrlEncode to encode the file name. I also remember that firefox did not need this. He also destroyed the file name when it is encoded in url. My code is:

 // IE needs url encoding, FF doesn't support it, Google Chrome doesn't care if (Request.Browser.IsBrowser ("IE")) { fileName = Server.UrlEncode(fileName); } Response.Clear (); Response.AddHeader ("content-disposition", String.Format ("attachment;filename=\"{0}\"", fileName)); Response.AddHeader ("Content-Length", data.Length.ToString (CultureInfo.InvariantCulture)); Response.ContentType = mimeType; Response.BinaryWrite(data); 

Edit

I read the specification more carefully. First of all, RFC2183 states that:

The current [RFC 2045] grammar restricts parameter values ​​(and therefore Content-Disposition file names) to US-ASCII.

But then I found links that [RFC 2045] is deprecated, and I need to reference RFC 2231 , which says:

Asterisks ("*") are reused to provide an indicator that language and character set information is present and encoding is being used. One quote ("'") is used to distinguish between the character set and language information at the beginning of the cost parameter. Percentage signs ("%") are used as the encoding flag, which is consistent with RFC 2047.

This means that you can use UrlEncode for non-ascii characters, as I said earlier, but you can also add a description of the encoding. Here is an example:

 string.Format("attachment; filename*=UTF-8''{0}", Server.UrlEncode(fileName)); 

I didn’t try, though ..

+12
source share

HttpUtility.UrlPathEncode might be a better option. Because URLEncode will replace spaces with + signs.

+5
source share

For me, this solution works in all major browsers:

 Response.AppendHeader("Content-Disposition", string.Format("attachment; filename*=UTF-8''{0}", HttpUtility.UrlPathEncode(fileName).Replace(",", "%2C")); var mime = MimeMapping.GetMimeMapping(fileName); return File(fileName, mime); 

Using ASP.NET MVC 3.

It is necessary to replace, because Chrome does not like Comma (,) in the parameter values: http://www.gangarasa.com/lets-Do-GoodCode/tag/err_response_headers_multiple_content_disposition/

+3
source share

You can read RFC 6266 and see the tests at http://greenbytes.de/tech/tc2231/ .

+2
source share

For me, this solved the problem:

 var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(data) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = "foo-ä-€.html" }; 

When I look at the repsonse declaration in fiddler, I see that the file name is automatically encoded using UTF-8:

Fiddler sample response with encoded content-Disposition filename using UTF-8

If we look at the value of the Content-Disposition header, we will see that it will be the same as @Johannes Geyer, its answer. The only difference is that we did not need to do ourselfs encoding, the ContentDispositionHeaderValue class will take care of this.

I used test files for the Content-Disposition header: http://greenbytes.de/tech/tc2231/ , as Julian Reshke mentioned. Information about the ContentDispositionHeaderValue class can be found on MSDN.

+1
source share

All Articles