So it works with HttpResponse class:

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

(We have to encode file name for IE) But now it should be done for HttpListener. It works for IE. The problem is FireFox and Chrome do not decode encoded header value like IE, but HttpResponse.AddHeader does not allow non-latin chars (code from System.Net):

if ((ch == '\x007f') || ((ch < ' ') && (ch != '\t')))
    throw new ArgumentException(SR.GetString("net_WebHeaderInvalidControlChars"), "value");

I tried to use Reflection to walk around the check:

Type type = response.Headers.GetType();
PropertyInfo info = type.GetProperty("InnerCollection",
    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic);
NameValueCollection headers = (NameValueCollection)info.GetValue(response.Headers, null);
headers.Add(name, value);

Nothing is risen but the file's name is totally corrupted. What should I do to make it work?

有帮助吗?

解决方案

There is no portable and cross-browser way to do this.

See a table at http://greenbytes.de/tech/tc2231/ for combinations of server/browser/headers that work and that don't.

Best bet is to send just Content-Disposition: attachment and let the browser to pick up the "default" file name from the /path/file.ext URI portion.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top