简体   繁体   English

Response.Write-Internet Explorer中的文件名编码错误

[英]Response.Write - filename encoding wrong in Internet Explorer

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. 问题是,当我从Internet Explorer下载文件时,特殊字符(如丹麦的æ,ø和å)被解释为错误。 So i file with the name 'Test æ ø å file.txt' downloads as 'Test æ_ø_Ã¥ file.txt' 因此,我将文件名为“ Testæøåfile.txt”下载为“ 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. 这似乎只是Internet Explorer中的问题。

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 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. 在检查标准之后,您可以改进代码以保留更多未编码的字符。

Try to specify specific file type like below (this is for excel file types) - 尝试指定特定的文件类型,如下所示(这是针对excel文件类型的)-

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

See What content type to force download of text response? 请参阅什么类型的内容可以强制下载文本响应?

It's much easier to get around the Internet Explorer filename problem, you'll find the answer here 解决Internet Explorer文件名问题要容易得多,您可以在此处找到答案

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). 您必须使用HttpUtility.UrlPathEncode(yourFileName) -因为这会弄乱其他浏览器中的文件名,因此您必须检查浏览器( Request.Browser.Browser )是否是Internet Explorer( IE直到版本10, Internet Explorer从版本11开始) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM