views:

176

answers:

4

When the file name is "Algunas MARCAS que nos acompañan" ASP.NET MVC raise an System.FormatExceptin when I try to download that file. But if the file name is "Asistente de Gerencia Comercial" it doesn't.

I guess this is because something related to UTF-8 encoding, but I don't know how to encode that string.

If I'm right, how can I encode the string in UTF-8 encoding? If I'm not right, what is my problem?

Thank you for any help.

+3  A: 

I encode file name like this for downloading,

     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
ZZ Coder
+1  A: 

Based on ZZ Coder answer, and because I'm using FileResult, I decided to encode the file name as:

HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)
eKek0
A: 

I recently fought with this a bit, having many potential languages being used for the file names (Chinese is good to test with). Here is something close to what I ended up with (other implementation details excluded):

HttpUtility.UrlEncode("é", System.Text.Encoding.GetEncoding("ISO-8859-1"))
John Fisher
A: 

This issue has been known for years. As far as I can tell, there currently is no interoperable way to do this, so the answer is to only support one set of browsers, or to do User Agent sniffing.

Test cases and links at: http://greenbytes.de/tech/tc2231/

Julian Reschke