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
<div style="MARGIN: 5px 20px 20px"> <div style="MARGIN-BOTTOM: 2px">Code:</div>
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);
}
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);
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.
suzi167
0 Points
21 Posts
fileContentResult and International characters
Jul 16, 2009 07:16 PM|LINK
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
<div style="MARGIN: 5px 20px 20px"> <div style="MARGIN-BOTTOM: 2px">Code:</div>
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); }</div>THanks in advance
Susan </div></div>
suzi167
0 Points
21 Posts
Re: fileContentResult and International characters
Jul 17, 2009 06:18 PM|LINK
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)
<div style="MARGIN-TOP: 10px" align=right mce_style="MARGIN-TOP: 10px">{
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;
}
levib
Star
7702 Points
1099 Posts
Microsoft
Re: fileContentResult and International characters
Jul 20, 2009 09:10 AM|LINK
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.