Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my File result filename corrupted IE9?

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?

like image 235
Joel Peltonen Avatar asked Nov 02 '22 02:11

Joel Peltonen


1 Answers

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);
}
like image 134
Joel Peltonen Avatar answered Nov 10 '22 18:11

Joel Peltonen