"attachment; filename="+HttpUtility.UrlEncode(fileName

时间:2022-04-10 02:51:36

一:TransmitFile文件下载,,不需要打开*文件,直接插入传输流中

Response.ContentType = "application/x-zip-compressed"; 

Response.AddHeader("Content-Disposition", 

"attachment;filename=z.zip"); 

string filename = Server.MapPath("DownLoad/z.zip"); 

Response.TransmitFile(filename);

二:

 

//

第二:WriteFile下载文件

string fileName ="asd.txt";//

客户端生存的文件名

string filePath=Server.MapPath("DownLoad/aaa.txt");//路径 

FileInfo fileInfo = new FileInfo(filePath); 

Response.Clear();    

Response.ClearContent();       

Response.ClearHeaders();       

Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);       

Response.AddHeader("Content-Length", fileInfo.Length.ToString());      

Response.AddHeader("Content-Transfer-Encoding", "binary");     

Response.ContentType = "application/octet-stream"; 

Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 

Response.WriteFile(fileInfo.FullName); 

Response.Flush();    

Response.End(); 

三:流方法下载

FileStream fs = new FileStream(filePath, FileMode.Open); 

byte[] bytes = new byte[(int)fs.Length];   

fs.Read(bytes, 0, bytes.Length);  

fs.Close(); 

Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开

Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 

Response.BinaryWrite(bytes); 

Response.Flush();   

Response.End();

四:WriteFile 分块下载 本身百度去