Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encode a file name for download?

When the file name is "Algunas MARCAS que nos acompañan" ASP.NET MVC raise an System.FormatException 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?

like image 409
eKek0 Avatar asked Oct 21 '09 02:10

eKek0


2 Answers

I encode file name like this for downloading,

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
like image 114
ZZ Coder Avatar answered Oct 23 '22 14:10

ZZ Coder


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)
like image 43
eKek0 Avatar answered Oct 23 '22 13:10

eKek0