.net几种文件下载的方法
.Net文件下载方式。。。。
之前在写上传文件、下载文件的时候,发现Response对象里面有好几种下载文件的方式,之后自己亲自实践了这几种方法,记录下以便以后复习。。。
WriteFile文件下载
1 public void getFile(string path, string fileName) 2 { 3 FileInfo fileInfo = new FileInfo(path); 4 Response.Clear(); 5 Response.ClearContent(); 6 Response.ClearHeaders(); 7 Response.ContentType = "application/octet-stream";//设置输出流的 HTTP MIME 类型 8 //Content-Disposition 为二进制格式提供已知MIME类型的“文件下载”对话框或建议动态内容的文件名的机会 9 Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));// URL 字符串进行编码,防止出现乱码 10 //回应消息体的长度,以 字节 (8位为一字节)为单位 11 Response.AddHeader("Content-Length", fileInfo.Length + ""); 12 //文件传输编码 二进制 13 Response.AddHeader("Content-Transfer-Encoding", "binary"); 14 //指定文件的内容作为文件块直接写入 HTTP 响应输出流 15 Response.WriteFile(fileInfo.FullName); 16 Response.Flush(); 17 Response.End(); 18 }
BinaryWrite流文件下载
1 public void getFile1(string path, string fileName) 2 { 3 FileStream stream = File.Open(path, FileMode.Open); // FileStream stream = new FileStream(path,FileMode.Open); 4 byte[] buffer = new byte[(int)stream.Length]; 5 stream.Read(buffer, 0, buffer.Length); 6 stream.Close();//文件流需要手动关闭,或者用using自动关闭 7 Response.Clear(); 8 Response.ContentType = "application/octet-stream"; 9 //通知浏览器下载 10 Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 11 //将二进制字符串写入 HTTP 输出流,已经是二进制了,就不需要 Response.AddHeader("Content-Transfer-Encoding", "binary"); 12 Response.BinaryWrite(buffer); 13 Response.Flush(); 14 Response.End(); 15 }
大文件分块下載
1 public void getFile2(string path, string fileName) 2 { 3 FileInfo fileInfo = new FileInfo(path); 4 if (fileInfo.Exists) 5 { 6 const long standardSize = 200*1024;//定义一个静态常量,每次读取200K,缓解服务器压力(大文件更好下载) 7 FileStream stream = File.OpenRead(path); 8 byte[] buffer = new byte[standardSize]; 9 long dataFielSize = stream.Length;//下载文件总大小 10 Response.Clear(); 11 Response.ContentType = "application/octet-stream"; 12 Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName)); 13 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 14 Response.AddHeader("Content-Transfer-Encoding", "binary"); 15 Response.ContentEncoding = System.Text.Encoding.Default; 16 if (dataFielSize > 0 && Response.IsClientConnected) 17 { 18 int readLengtn = stream.Read(buffer, 0, (int)standardSize); 19 Response.OutputStream.Write(buffer, 0, readLengtn); 20 Response.Flush(); 21 dataFielSize = dataFielSize - readLengtn; 22 } 23 stream.Close(); 24 Response.Close(); 25 } 26 }
TransmitFile文件下载
1 public void getFile4(string path, string fileName) 2 { 3 Response.Clear(); 4 Response.ContentType = "application/octet-stream"; 5 Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName)); 6 Response.TransmitFile(path); 7 }
JSWindow打开文件
1 public void getFile3(string path) 2 { 3 Response.Write("<script>window.open('" + path + "')</script>");//文件打开可以下载,图片打开就是在线预览 4 }
方法调用函数如下:
1 protected void btnn_Click(object sender, EventArgs e) 2 { 3 Button btn = (Button)sender; 4 //var aa = Request.MapPath("../Pages/uploadfiles");//Request对象获取绝对路径 5 var filesArr = Directory.GetFiles(Server.MapPath("../Pages/uploadfiles"));//Server对象获取绝对路径 6 var tempArr = filesArr[1].Split('\\'); 7 var jsdownLoadUrl = "../Pages/uploadfiles/" + tempArr[tempArr.Length - 1]; 8 var fileName = "图片下载"; 9 switch (btn.ID) 10 { 11 case "btn1": 12 getFile(filesArr[0], fileName); 13 break; 14 case "btn2": 15 getFile1(filesArr[0], fileName); 16 break; 17 case "btn3": 18 getFile2(filesArr[0], fileName); 19 break; 20 case "btn4": 21 getFile3(jsdownLoadUrl); 22 break; 23 case "btn5": 24 getFile4(filesArr[0], fileName); 25 break; 26 default: 27 break; 28 } 29 30 }
本文只是记录学习心得,如果有误请提示博主加以修正。。。
谢谢您那么帅气,还能看完小主的文言文!