2

I use the following code to send files from my server to the client:

Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
Response.ContentType = MimeType;
Response.WriteFile(PathToFile);
Response.End();

This works fine. Problem is, that when I download files from Internet Explorer, special characters, like the danish æ, ø and å, gets interpreted wrong. So i file with the name 'Test æ ø å file.txt' downloads as 'Test æ_ø_Ã¥ file.txt'

I´ve tried adding Byte Order Mark to the response:

byte[] BOM = { 0xEF, 0xBB, 0xBF };
Response.BinaryWrite(BOM);

And setting the charset:

Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;

But none if it helped. This seems only to be a problem in Internet Explorer.

Kara
  • 6,115
  • 16
  • 50
  • 57
Farsen
  • 1,387
  • 3
  • 21
  • 48

3 Answers3

4

The headers doesn't use the encoding of the content, so it won't help to change the content encoding. There is a standard for specifying encoding for the header parameters, but it's pretty new, so it won't be fully supportred in all browsers:

http://greenbytes.de/tech/webdav/rfc5987.html

This is how you would encode the name:

string FileName = "Test æ ø å file.txt";

string name = String.Concat(Encoding.UTF8.GetBytes(FileName).Select(b => {
  if ((b >= 48 && b <= 57) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122)) {
    return new String((char)b, 1);
  } else {
    return String.Format("%{0:x2}", b);
  }
}).ToArray());

Response.AppendHeader("content-disposition", "attachment; filename*=UTF-8''" + name);

The text ending up in the header would look like this:

attachment; filename*=UTF-8''Test%20%c3%a6%20%c3%b8%20%c3%a5%20file%2etxt

Note: The code encodes all characters except alphanumeric. That works, but not all other characters need encoding. You could evolve the code to leave a few more characters unencoded, after checking the standards.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks alot for the quick reply. I see what you mean, and Ive tried your example code, which removes the IE filename problem. But in other browsers, the filename is now 'Test%20%c3%a6%20%c3%b8%20%c3%a5%20file%2etxt'. So should I treat the filename based on what browser requests the file, or isnt there a general solution? – Farsen Jan 21 '13 at 10:44
  • 1
    @Farsen: It seems that the support is lacking, so different code for different browsers would be your only option. For older IE browsers you would need to normalise the file name as they don't support either way of using Unicode. – Guffa Jan 21 '13 at 10:53
  • There is a special class 'ContentDisposition' in .NET. Check this answer: http://stackoverflow.com/a/30972670/155687 – Vladislav Sep 13 '16 at 11:37
1

Try to specify specific file type like below (this is for excel file types) -

Response.ContentType = "application/vnd.ms-excel";

See What content type to force download of text response?

Community
  • 1
  • 1
Umesh
  • 2,704
  • 19
  • 21
  • I already set the content type as the officiel mimetypes, and no matter what type it is, the encoding is still wrong :( – Farsen Jan 21 '13 at 10:49
0

It's much easier to get around the Internet Explorer filename problem, you'll find the answer here

You have to HttpUtility.UrlPathEncode(yourFileName) - as this will mess up filenames in other browser, you'll have to check if the browser (Request.Browser.Browser) is Internet Explorer (IE until version 10, Internet Explorer from version 11).

Community
  • 1
  • 1
outofmind
  • 1,430
  • 1
  • 20
  • 37