Response.Write - Internet Explorer中的文件名编码错误 [英] Response.Write - filename encoding wrong in Internet Explorer

查看:169
本文介绍了Response.Write - Internet Explorer中的文件名编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这工作正常。问题是,当我从Internet Explorer下载文件时,特殊字符,如丹麦æ,ø和å,被解释错误。所以我以Testæøåfile.txt的名称下载为'TestÃ|_ø_Ã¥file.txt'



我尝试添加Byte订单标记为响应:

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

并设置字符集:

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

但是没有帮助。
这似乎只是Internet Explorer中的一个问题。

解决方案

标题不使用内容,所以不会改变内容的编码。有一个标准的标题参数的编码,但它是相当新的,所以它不会完全支持所有浏览器:



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



这是你如何编码的名字:

  string FileName =Testæøåfile.txt ; 

string name = String.Concat(Encoding.UTF8.GetBytes(FileName).Select(b => {
if((b> = 48&& (b> = 97&& b< = 122)){
返回新的String(( char)b,1);
} else {
return String.Format(%{0:x2},b);
}
})ToArray() );

Response.AppendHeader(content-disposition,attachment; filename * = UTF-8+名称);

标题中的文本将如下所示:

 附件;文件名* = UTF-8''Test%20%c3%a6%20%c3%b8%20%c3%a5%20file%2etxt 

注意:代码对除字母数字以外的所有字符进行编码。这有效,但并不是所有其他字符都需要编码。检查标准后,您可以将代码进化为不再编码的字符。


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.

解决方案

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.

这篇关于Response.Write - Internet Explorer中的文件名编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆