Valid filenames (for download) in IE?

asp.net, asp.net-mvc, http, internet-explorer, unicode

Solution

`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.

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`.

Problem

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)

Original source

Related problems