Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

asp.net - Valid filenames (for download) in IE?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. UrlEncode is misleadingly-named, it is only for form data. You would want UrlPathEncode instead which would solve the + problem. See this question for background.

  2. 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 the Content-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 and UrlPathEncode). eg for someaction 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...