对文件名使用UTF8编码,代码如下:
[HttpGet]
public ActionResult FileDown(Int64 id)
{
Order entity = bll.GetModel(id);
string contentType = "application/x-jpg";
if (System.IO.Path.GetExtension(entity.OFileName).ToLower().Equals(".bmp"))
{
contentType = "application/x-bmp";
}
else if (System.IO.Path.GetExtension(entity.OFileName).ToLower().Equals(".pdf"))
{
contentType = "application/pdf";
}
else if (System.IO.Path.GetExtension(entity.OFileName).ToLower().Equals(".doc"))
{
contentType = "application/msword";
}
//火狐浏览器不需要转换fileName,否则中文文件名易出现乱码
if (Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1)
{
entity.OFileName = System.Web.HttpUtility.UrlEncode(entity.OFileName, System.Text.Encoding.UTF8);
}
return File(entity.OrderFile, contentType, entity.OFileName);
}
本文介绍了一种根据文件扩展名设置MIME类型,并针对不同浏览器处理中文文件名乱码问题的方法。通过检测用户代理来判断是否需要对文件名进行UTF-8编码,确保文件能正确下载。
1372

被折叠的 条评论
为什么被折叠?



