在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法
[CheckSession]
public ActionResult ExpDataTwo()
{
try
{
string Status_Missionn = Request.QueryString["sexch"];
ParadoxWebService.ParadoxServiceClient client = new ParadoxWebService.ParadoxServiceClient();
ExportResultInfo res = client.ExportParadoxDataBase(LoadExportSqlStrTwo(Status_Missionn), "GLJ");
string XMlDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Download/");
string fileName = Session["hangji"].ToString() + Session["atmTwo_Code"].ToString() + DateTime.Now.ToString("yyyyMMddmmss") + ".DB"; // 以分秒为文件名。
# region 处理乱码
Encoding encoding;
string outputFileName = null;
string browser = Request.UserAgent.ToUpper();
if (browser.Contains("MS") == true && browser.Contains("IE") == true)
{
outputFileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20");
encoding = System.Text.Encoding.Default;
}
else if (browser.Contains("FIREFOX") == true)
{
outputFileName = fileName;
encoding = System.Text.Encoding.GetEncoding("GB2312");
}
else
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = System.Text.Encoding.Default;
}
#endregion
string url = XMlDirectory + fileName;
ConvertToDB(res.ExportDbBuffer, url);
return File(url, "application/vnd.borland", outputFileName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 字节转化为文件。
/// </summary>
/// <param name="buffer"></param>
private void ConvertToDB(byte[] buffer, string filePath)
{
FileStream fstream = System.IO.File.Create(filePath, buffer.Length);
try
{
fstream.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
}
finally
{
fstream.Close();
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。