5

I'm trying to generate and return files for my users. Some file names contain unicode characters: aäa.pdf. When trying to download them in IE 9 the filename gets corrupted and the download appears like this:

         Ie9 filename issue

In Chrome it works as expected. This is the return statement:

return File(fileStream: stream, 
            contentType: System.Net.Mime.MediaTypeNames.Application.Octet, 
            fileDownloadName: myModel.Name + "." + pub.OutputFileType);

How can I fix the issue that IE 9 has?

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • There isn't any good cross browser solution exsist for this problem: http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http?lq=1 also http://stackoverflow.com/questions/8551522/valid-filenames-for-download-in-ie and http://stackoverflow.com/questions/695356/downloading-file-with-or-in-file-name-ruins-filename/695719#695719 – nemesv Mar 27 '14 at 16:24

1 Answers1

2

It seems that this is simply no doable with IE9, as state by namesv in the comments. I finally solved it by abusing a browser feature: when it requests something like /foo/fileÄÖ.pdf and a file is returned, the "filename" in the URL is used.

So, I created a new route, targeted my dynamic download to it and did this in the action:

if (Request.Browser.Browser == "IE" && Request.Browser.MajorVersion == 9)
{
    // The filename comes from the Route used, which is like host/Print/File.pdf
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment");
    Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;
    Response.Charset = "utf-8";
    Response.HeaderEncoding = UnicodeEncoding.UTF8;
    Response.ContentEncoding = UnicodeEncoding.UTF8;
    byte[] buffer = new byte[4096];
    int count = 0;

    while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        Response.OutputStream.Write(buffer, 0, count);
        Response.Flush();
    }
    Response.End();
    return null;
}
else
{
    return File(
        fileStream: stream,
        contentType: System.Net.Mime.MediaTypeNames.Application.Octet,
        fileDownloadName: compilation.Name + "." + pub.OutputFileType);
}
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100