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)
UrlEncodeis misleadingly-named, it is only for form data. You would wantUrlPathEncodeinstead 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-Dispositionfilename 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.Drop the
filenameparameter from theContent-Dispositionheader 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 forsomeactioncontroller:All browsers will offer to save the resulting file as
åäöü.txt.