有效的文件名(下载)在IE浏览器? [英] Valid filenames (for download) in IE?
问题描述
我在ASPNET MVC返回一个FileContentResult的动作。我注意到,当fileDownloadName包含变音(即亚洲开放大学协会)Internet Explorer无法在所有读取的文件名。
我已经试过URL编码:
返回this.File(document.Content,则contentType,Server.UrlEn code(document.Name));
但随后所有的空格用加号(+)取代。
有没有办法让UNI code文件名与IE浏览器(保持原文件名不变)?
这是我目前使用作为一个黑客:
返回this.File(
document.Content,
内容类型,
Server.UrlEn code(document.Name).Replace('+',''));
(这使得空间在IE下划线)
-
UrlEn code
被误导命名,这只是形式的数据。你想UrlPathEn code
,而不是将它解决了+
问题。请参见背景这个问题。 -
然而,在URL编码无论如何是错误的做法在这里,你不是构造URL。它工作在IE其实是一个错误,这就是为什么你会得到
%
-sequences在其他浏览器。
不幸的是,没有可靠的跨浏览器的方式来获得非ASCII字符变成内容处置
文件名参数。在理论上它可能使用RFC 2331的规则都没有可能,但即使如此,该规范是值得商榷和现实是什么支持它。请参见背景这个问题。
有没有办法让UNI code文件名与IE(保持原文件名不变)?
工作
块引用>删除该
文件名
从内容处置
头,而是包括文件名作为尾随路部分参数脚本的地址,它是相当有效的使用URL编码(UTF-8和UrlPathEn code
)。例如,对于someaction
控制器:http://www.example.com/someaction/åäöü.txt
http://www.example.com/someaction/%C3%A5%C3%A4%C3%B6%C3%BC.txt所有的浏览器将提供以保存生成的文件作为
åäöü.txt
。I have an action in aspnet mvc that returns a FileContentResult. I have noticed that when the fileDownloadName contains umlauts (ie åäöü) Internet Explorer can't read the file name at all.
I have tried UrlEncoding:
return this.File(document.Content, contentType, Server.UrlEncode(document.Name));
but then all spaces are replaced by plus signs (+).
Is there a way to get unicode file names work with IE (keeping the original file name intact)?
This is what I'm currently using as a hack:
return this.File( document.Content, contentType, Server.UrlEncode(document.Name).Replace('+',' '));
(This renders space as an underscore in IE)
解决方案
UrlEncode
is misleadingly-named, it is only for form data. You would wantUrlPathEncode
instead which would solve the+
problem. See this question for background.However in any case URL-encoding is the wrong thing to do here, as you aren't constructing a URL. The fact that it works in IE is a bug, which is why you get
%
-sequences in other browsers.Unfortunately, there is no reliable cross-browser way to get non-ASCII characters into a
Content-Disposition
filename parameter. In theory it might have been possible using RFC 2331 rules, but even then the spec is arguable and the reality is nothing supports it. See this question for background.Is there a way to get unicode file names work with IE (keeping the original file name intact)?
Drop the
filename
parameter from theContent-Disposition
header and instead include the filename as a trailing path part of your script's address, where it's quite valid to use URL-encoding (UTF-8 andUrlPathEncode
). eg forsomeaction
controller:http://www.example.com/someaction/åäöü.txt http://www.example.com/someaction/%C3%A5%C3%A4%C3%B6%C3%BC.txt
All browsers will offer to save the resulting file as
åäöü.txt
.这篇关于有效的文件名(下载)在IE浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!