ASP MVC3 FileResult with accents + IE8 - bugged?
Asked Answered
D

2

5

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);
        }
    }
}
Downstairs answered 4/8, 2011 at 14:41 Comment(0)
E
2

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.

Eggplant answered 4/8, 2011 at 15:1 Comment(3)
Doesn't seem to work, file name in the header is still UTF8 encoded: Content-Disposition: attachment; filename*=UTF-8''dsa%C3%A9ee.txtDownstairs
@Akos Lukacs, how about the other methods proposed in the blog post?Eggplant
Checking the browser, and sending different headers actually solves the problem, thanks.Downstairs
M
0

Is this a file the user is uploading to your system at some point? If so, restrict the use of accents in a file name. If not - don't use accents in your file names :).

Mythopoeic answered 4/8, 2011 at 14:52 Comment(3)
Not using accents is not really an option. As a developer I know accents, space, etc are problematic. But living in a country where accents are used every day, I can't say "no accents!"...Downstairs
I know this is a rather simplistic answer, but I like to KISS. Otherwise I will bang my head for hours on a problem that can take 15 minutes to fix.Mythopoeic
Btw, the file list comes from an external system, and users could upload/modify files from that external system. So the most I could do is check if the browser is IE 8, or worse, and replace the special characters when sending the file to download. But still that could cause other problems, since if they re-upload the file, file name is changed...Downstairs

© 2022 - 2024 — McMap. All rights reserved.