In my ApiController class, I have following method to download a file created by server.
public HttpResponseMessage Get(int id)
{
try
{
strin
Considering the previous answers, it is necessary to be careful with globalized characters.
Suppose the name of the file is: "Esdrújula prenda ñame - güena.jpg"
Raw result to download: "Esdrújula prenda ñame - güena.jpg" [Ugly]
HtmlEncode result to download: "Esdr&_250;jula prenda &_241;ame - g&_252;ena.jpg" [Ugly]
UrlEncode result to download: "Esdrújula+prenda+ñame+-+güena.jpg" [OK]
Then, you need almost always to use the UrlEncode over the file name. Moreover, if you set the content-disposition header as direct string, then you need to ensure surround with quotes to avoid browser compatibility issues.
Response.AddHeader("Content-Disposition", $"attachment; filename=\"{HttpUtility.UrlEncode(YourFilename)}\"");
or with class aid:
var cd = new ContentDisposition("attachment") { FileName = HttpUtility.UrlEncode(resultFileName) };
Response.AddHeader("Content-Disposition", cd.ToString());
The System.Net.Mime.ContentDisposition class takes care of quotes.