Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC3 FileResult with accents + IE8 - bugged?

If the file name contains accents, it works as expected in Opera, FF, Chrome and IE9.

But in IE8 file type is "unknown file type", and shows "file" as the file name (actually the last part of the URL).

Does anyone know a workaround? Other than replacing the "special" characters in the file name?

The test code: (file | new project | add controller)

public class FileController : Controller
{
    public ActionResult Index(bool? Accents)
    {
        byte[] content = new byte[] { 1, 2, 3, 4 };

        return File(content, "application/octet-stream", true.Equals(Accents) ? "dsaé.txt" : "dsae.txt");
    }
}

test it like this: http://localhost/file, and http://localhost/file?accents=true

Edit => The "solution" for me, if anyone interested:

public class FileContentResultStupidIE : FileContentResult //yeah, maybe i am not totally "politically correct", but still...
{
    public FileContentResultStupidIE(byte[] fileContents, string contentType) : base(fileContents, contentType) { }

    public override void ExecuteResult(ControllerContext context)
    {
        var b = context.HttpContext.Request.Browser;
        if (b != null && b.Browser.Equals("ie", StringComparison.OrdinalIgnoreCase) && b.MajorVersion <= 8)
        {
            context.HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(base.FileDownloadName) + "\"");
            WriteFile(context.HttpContext.Response);
        }
        else
        {
            base.ExecuteResult(context);
        }
    }
}
like image 769
Akos Lukacs Avatar asked Nov 05 '22 16:11

Akos Lukacs


1 Answers

Try adding the following line inside your controller action:

Response.HeaderEncoding = Encoding.GetEncoding("iso-8859-1");

You may take a look at the following blog post which discusses those issues. Unfortunately there isn't a general solution which will work among all browsers.

like image 155
Darin Dimitrov Avatar answered Nov 11 '22 16:11

Darin Dimitrov