我在aspnet mvc中有一个返回FileContentResult的动作。我注意到当fileDownloadName包含变音符号(即åäöü)时,Internet Explorer根本无法读取文件名。
我试过UrlEncoding:
return this.File(document.Content, contentType, Server.UrlEncode(document.Name));
但随后所有空格都被加号(+)替换。
有没有办法让unicode文件名与IE一起使用(保持原始文件名不变)?
这就是我目前正在使用的黑客行为:
return this.File(
document.Content,
contentType,
Server.UrlEncode(document.Name).Replace('+',' '));
(这在IE中将空格渲染为下划线)
答案 0 :(得分:6)
UrlEncode
具有误导性名称,仅适用于表单数据。您可能需要UrlPathEncode
来解决+
问题。有关背景信息,请参阅this question。
但是,无论如何,URL编码在这里是错误的,因为您没有构建URL。它在IE中运行的事实是一个错误,这就是为什么你在其他浏览器中获得%
- 序列的原因。
不幸的是,没有可靠的跨浏览器方式将非ASCII字符转换为Content-Disposition
文件名参数。从理论上讲,使用RFC 2331规则可能是可能的,但即便如此,规范也是有争议的,现实并没有支持它。有关背景信息,请参阅this question。
有没有办法让unicode文件名与IE一起工作(保持原始文件名不变)?
从filename
标题中删除Content-Disposition
参数,而是将文件名作为脚本地址的尾随路径部分包含在内,使用URL编码非常有效(UTF-8和{{ 1}})。例如,对于UrlPathEncode
控制器:
someaction
所有浏览器都会将结果文件保存为http://www.example.com/someaction/åäöü.txt
http://www.example.com/someaction/%C3%A5%C3%A4%C3%B6%C3%BC.txt
。
答案 1 :(得分:1)
对于回复,我使用来自this thread的解决方案。也许你可以试着像
一样使用它return this.File(
document.Content,
contentType,
"\"" + Server.UrlEncode(document.Name) + "\"");
或
return this.File(
document.Content,
contentType,
"\"" + document.Name + "\"");
答案 2 :(得分:0)
只需按照您的方式对文件名进行编码,然后将所有+
替换为%20
。未经测试,但应该工作。
答案 3 :(得分:0)
我已尝试过编码内容,在Chrome和FF中运行良好,但在IE上遇到了麻烦。 所以我想出了自己的清洁程序。基本上它只是一个映射表,因为我们不只是想删除所有特殊字符。
//http://www.pjb.com.au/comp/diacritics.html
private static string[,] CharacterReplacements = {
{ " ", "-"},
{ "&", "-"},
{ "?", "-"},
{ "!", "-"},
{ "%", "-"},
{ "+", "-"},
{ "#", "-"},
{ ":", "-"},
{ ";", "-"},
{ ".", "-"},
{ "¢", "c" }, //cent
{ "£", "P" }, //Pound
{ "€", "E" }, //Euro
{ "¥", "Y" }, //Yen
{ "°", "d" }, //degree
{ "¼", "1-4" }, //fraction one-quarter
{ "½", "1-2" }, //fraction half
{ "¾", "1-3" }, //fraction three-quarters}
{ "@", "AT)"}, //at
{ "Œ", "OE" }, //OE ligature, French (in ISO-8859-15)
{ "œ", "oe" }, //OE ligature, French (in ISO-8859-15)
{"Å","A" }, //ring
{"Æ","AE"}, //diphthong
{"Ç","C" }, //cedilla
{"È","E" }, //grave accent
{"É","E" }, //acute accent
{"Ê","E" }, //circumflex accent
{"Ë","E" }, //umlaut mark
{"Ì","I" }, //grave accent
{"Í","I" }, //acute accent
{"Î","I" }, //circumflex accent
{"Ï","I" }, //umlaut mark
{"Ð","Eth"}, //Icelandic
{"Ñ","N" }, //tilde
{"Ò","O" }, //grave accent
{"Ó","O" }, //acute accent
{"Ô","O" }, //circumflex accent
{"Õ","O" }, //tilde
{"Ö","O" }, //umlaut mark
{"Ø","O" }, //slash
{"Ù","U" }, //grave accent
{"Ú","U" }, //acute accent
{"Û","U" }, //circumflex accent
{"Ü","U" }, //umlaut mark
{"Ý","Y" }, //acute accent
{"Þ","eth"}, //Icelandic - http://en.wikipedia.org/wiki/Thorn_(letter)
{"ß","ss"}, //German
{"à","a" }, //grave accent
{"á","a" }, //acute accent
{"â","a" }, //circumflex accent
{"ã","a" }, //tilde
{"ä","ae"}, //umlaut mark
{"å","a" }, //ring
{"æ","ae"}, //diphthong
{"ç","c" }, //cedilla
{"è","e" }, //grave accent
{"é","e" }, //acute accent
{"ê","e" }, //circumflex accent
{"ë","e" }, //umlaut mark
{"ì","i" }, //grave accent
{"í","i" }, //acute accent
{"î","i" }, //circumflex accent
{"ï","i" }, //umlaut mark
{"ð","eth"}, //Icelandic
{"ñ","n" }, //tilde
{"ò","o" }, //grave accent
{"ó","o" }, //acute accent
{"ô","o" }, //circumflex accent
{"õ","o" }, //tilde
{"ö","oe"}, //umlaut mark
{"ø","o" }, //slash
{"ù","u" }, //grave accent
{"ú","u" }, //acute accent
{"û","u" }, //circumflex accent
{"ü","ue"}, //umlaut mark
{"ý","y" }, //acute accent
{"þ","eth"}, //Icelandic - http://en.wikipedia.org/wiki/Thorn_(letter)
{"ÿ","y" }, //umlaut mark
};
我把它包装成一个函数,你可以在这里找到代码:
http://remy.supertext.ch/2012/08/clean-filenames/