Attachment downloaded with special character/strange filename

Nice solution posted in StackOverflow
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

Leave a Reply

Your email address will not be published. Required fields are marked *