1 public static void DownLoadFile(string fileName, HttpResponse Response, HttpServerUtility Server) 2 { 3 string path = Server.MapPath("~/UpFiles/" + fileName); 4 FileInfo fileInfo = new FileInfo(path); 5 6 //TextWriter writer = new StreamWriter(defultPath); 7 //HttpResponse Response = new HttpResponse(writer); 8 9 Response.Clear(); 10 11 Response.ClearContent(); 12 13 Response.ClearHeaders(); 14 15 Response.AppendHeader("Content-Disposition", "attachment; filename="" + Server.UrlEncode(fileName) + """); 16 17 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 18 19 Response.AddHeader("Content-Transfer-Encoding", "binary"); 20 21 Response.ContentType = "application/octet-stream"; 22 23 Response.ContentEncoding = System.Text.Encoding.UTF8; 24 25 Response.WriteFile(fileInfo.FullName); 26 27 Response.Flush(); 28 29 Response.End(); 30 }
已有文档下载。
防中文乱码:Server.UrlEncode(fileName)