tags:

views:

254

answers:

3

Hello,

I am using a fileContentResult to render a file to the browser. It works well except that it throws an exception when the fileName contains international characters. I remember reading somewhere that this feature does not support international characters but I am sure there mustbe a workaround or a best practice people follow in cases the application needs to upload files in countries other than US.

Does anyone know of such a practice?Here is the ActionResult Method

public ActionResult GetFile(byte[] value, string fileName)
    {
        string fileExtension = Path.GetExtension(fileName);
        string contentType = GetContentType(fileExtension); //gets the content Type
        return File(value, contentType, fileName);
    }

THanks in advance

Susan

A: 

I don't think it's possible to download files with international characters in the file name. The file name is part of the Content-disposition header, and like all HTTP headers, there's no way of using a different encoding other than ASCII that will work across all browsers and proxies.

Uploading files with international characters should be no problem, though, since the file name is transmitted as normal form data (application/www-url-encoded)

chris166
I know that but I tried it on a few websites and it works well with Swedish characters so there must be an alternate solution. For example if you attach a file to gmail and download it even if it has international chars it still works well. One thing I can think of is Attaching the file directly to the response but then how would I send it back to the client in MVC?
suzi167
You can try to reverse-engineer how Google does it and create your own class derived from ActionResult, where you can return whatever you want (limited by what ASP.NET allows you to do).
chris166
A: 

I think it depends on your responseHeaderEncoding (see http://msdn.microsoft.com/en-us/library/hy4kkhe0.aspx )

HTH,

Erik

Erik
A: 
public class UnicodeFileContentResult : ActionResult {

    public UnicodeFileContentResult(byte[] fileContents, string contentType) {
        if (fileContents == null || string.IsNullOrEmpty(contentType)) {
            throw new ArgumentNullException();
        }

        FileContents = fileContents;
        ContentType = contentType;
    }

    public override void ExecuteResult(ControllerContext context) {
        var encoding = UnicodeEncoding.UTF8;
        var request = context.HttpContext.Request;
        var response = context.HttpContext.Response;

        response.Clear();
        response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", (request.Browser.Browser == "IE") ? HttpUtility.UrlEncode(FileDownloadName, encoding) : FileDownloadName));
        response.ContentType = ContentType;
        response.Charset = encoding.WebName;
        response.HeaderEncoding = encoding;
        response.ContentEncoding = encoding;
        response.BinaryWrite(FileContents);
        response.End();
    }

    public byte[] FileContents { get; private set; }

    public string ContentType { get; private set; }

    public string FileDownloadName { get; set; }
}
AlexMAS