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", "attachment; filename=" + Server.UrlEncode(file.Name)); //解決中文文件名亂碼
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章