fileContentResult and International characters

Last post 07-20-2009 5:10 AM by levib. 2 replies.

Sort Posts:

  • fileContentResult and International characters

    07-16-2009, 3:16 PM
    • Member
      point Member
    • suzi167
    • Member since 04-03-2007, 8:10 PM
    • Posts 21
    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 download files in countries other than US. and the file names contain non-English characters.
    Does anyone know of such a practice?Here is the ActionResult Method

    Code:
        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
  • Re: fileContentResult and International characters

    07-17-2009, 2:18 PM
    Answer
    • Member
      point Member
    • suzi167
    • Member since 04-03-2007, 8:10 PM
    • Posts 21

    THe solution actually is not to use the FIle() method since it does not support international characters in the file name;

    Instead use the old-school Response to render the file back to the Browser:

    Check out my solution below:

     

    public ActionResult GetFile(byte[] value, string fileName)
    {
    string fileExtension = Path.GetExtension(fileName);
    string contentType = GetContentType(fileExtension);

    Response.Clear();
    if (Request.Browser.Browser == "IE")//IE needs special handling in order to display the international
    //characters in the file name
    {
    string attachment = String.Format("attachment; filename={0}", Server.UrlPathEncode(fileName));
    Response.AddHeader("Content-Disposition", attachment);
    }
    else
    Response.AddHeader("Content-Disposition", "attachment; filename="+fileName);

    Response.ContentType = contentType;
    Response.Charset = "utf-8";
    Response.HeaderEncoding = UnicodeEncoding.UTF8;
    Response.ContentEncoding = UnicodeEncoding.UTF8;
    Response.BinaryWrite(value);
    Response.End();
    return null;

    }

     

  • Re: fileContentResult and International characters

    07-20-2009, 5:10 AM
    • Contributor
      5,131 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 842

    The .NET framework supports RFC 2183 (http://www.ietf.org/rfc/rfc2183.txt) for the MIME Content-Disposition header.  Sec. 2.3 of this RFC explicitly restricts valid filename characters to US-ASCII.  There is an additional RFC 2231 (http://tools.ietf.org/html/rfc2231) which supports international character sets, but this is not implemented by the .NET framework.

    The team responsible for the MIME code is aware of this issue and may act on it in the future.  We may also provide a workaround in a future version of MVC.  I can't guarantee either of these, though.  In the meantime, you may want to implement RFC 2231 manually to generate the Content-Disposition header.

Page 1 of 1 (3 items)