当前位置:首页 > Windows程序 > 正文

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

2024-03-31 Windows程序

一: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 分块下载 本身百度去

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/33864.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494587访问次数
  • 建站天数
  • 友情链接