Reza
Reza

Reputation: 19843

FileContentResult and international characters in the file name

I am using this code to return the file to the client

return new FileContentResult(Encoding.UTF8.GetBytes(sb.ToString()), MimeType)
            {
                FileDownloadName = String.Format("{0}.csv", fileName)
            };

it works file when the file name is in ascii format, but it will return the name of the Action when the file name contains international chars

For example if filename is Report 新しいレポート the downloaded file is the name of the Action without any extension.

Update

This will happen in Chrome and ie, in Firefox the file is downloaded but some chars are changed

Firefox: Report2%0d%0a ��しいレポート_2016-03-09_09-20-35.c%0d%0a sv

Upvotes: 2

Views: 2958

Answers (1)

Reza
Reza

Reputation: 19843

For whom who have the same problem:

var browser = System.Web.HttpContext.Current.Request.Browser.Browser;
if (browser == "Chrome" || browser == "IE" || browser == "InternetExplorer")
{
     fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
}

return new FileContentResult(Encoding.UTF8.GetBytes(sb.ToString()), MimeType)
{
      FileDownloadName = String.Format("{0}.csv", fileName)
};

Upvotes: 3

Related Questions