Content-Disposition Special Characters

asp.net-mvc-2content-dispositioninternationalizationutf-8

I have encountered an issue when trying to serve files with spanish tildes, related to the Content-Disposition encoding.

The file name is "qué hacés ahora.docx"

So far, the ASP.NET MVC way of serving files adds this header, which works fine only in Firefox:

Content-Disposition: attachment; filename*=UTF-8''qu%C3%A9%20hac%C3%A9s%20ahora.docx

I am using in the controller:

return File(path, "application/octet-stream", originalNameWithAccents);

This is not working in IE or Chrome.

So, I went and emailed myself the file as an attachment using GMail. Using firebug to see what is going on, the Content-Disposition google is sending back down is:

Content-Disposition: attachment; filename="=?UTF-8?B?cXXDqSBoYWPDqXMgYWhvcmEuZG9jeA==?="

Notice how the name is base64 encoded (cXXDqSBoYWPDqXMgYWhvcmEuZG9jeA==).

So, I have 2 questions so far:

  1. Where is an specification for this?
  2. Are there any known ways of serving this type filename using ASP.NET?

Best Answer

I had the same problem with the file name. File name like

 filename="=?utf-8?B?VGVzdG93eVBMSUvFgsSFw7PEh2FwaS5kb2N4?="

is encoded in base64 (VGVzdG93eVBMSUvFgsSFw7PEh2FwaS5kb2N4) charset utf-8 and oryginal file name:

 TestowyPLIKłąóćapi.docx

This is way to get file name:

string cdString = "attachment; filename=\"=?UTF-8?B?cXXDqSBoYWPDqXMgYWhvcmEuZG9jeA==?=\"";
string filename;

System.Net.Http.Headers.ContentDispositionHeaderValue val;
if (System.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(cdString, out val))
    fileName = val.FileName.Replace("\"", "");
else
    throw new Exception("...");

And

fileName = "qué hacés ahora.docx"
Related Topic