Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, July 16, 2009 3:16 PM
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
<!-- / message -->
<!-- / message -->
All replies (2)
Friday, July 17, 2009 2:18 PM âś…Answered
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;
} <!-- / message -->
<!-- controls -->
Monday, July 20, 2009 5:10 AM
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.