Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...