asp.net后台下载文件(解决中文乱码)

asp.net后台下载文件(解决中文乱码)

 你是否在asp.net开发中遇到过下载文件时乱码的现象,也许这个代码不是最好的,但是它能帮你下载文件并且解决中文乱码的问题

private void DownLoadFile(string fileName)

    {  
string filePath = Server.MapPath(".") + "\\" + fileName;
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}