博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】C# 从服务器下载文件
阅读量:5319 次
发布时间:2019-06-14

本文共 5136 字,大约阅读时间需要 17 分钟。

支持并尊重原创!原文地址:
1 一、//TransmitFile实现下载   2     protected void Button1_Click(object sender, EventArgs e)  3     {  4         /*   5         微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite   6         下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。   7         代码如下:   8         */   9         Response.ContentType = "application/x-zip-compressed"; 10         Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); 11         string filename = Server.MapPath("DownLoad/z.zip"); 12         Response.TransmitFile(filename); 13     } 14  15 二、//WriteFile实现下载  16     protected void Button2_Click(object sender, EventArgs e) 17     { 18         /*  19         using System.IO; 20  21         */ 22         string fileName = "asd.txt";//客户端保存的文件名  23         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 24  25         FileInfo fileInfo = new FileInfo(filePath); 26         Response.Clear(); 27         Response.ClearContent(); 28         Response.ClearHeaders(); 29         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 30         Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 31         Response.AddHeader("Content-Transfer-Encoding", "binary"); 32         Response.ContentType = "application/octet-stream"; 33         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 34         Response.WriteFile(fileInfo.FullName); 35         Response.Flush(); 36         Response.End(); 37     } 38  39 三、   //WriteFile分块下载  40     protected void Button3_Click(object sender, EventArgs e) 41     { 42         string fileName = "aaa.txt";//客户端保存的文件名  43         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 44  45         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); 46  47         if (fileInfo.Exists == true) 48         { 49             const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力  50             byte[] buffer = new byte[ChunkSize]; 51  52             Response.Clear(); 53             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); 54             long dataLengthToRead = iStream.Length;//获取下载的文件总大小  55             Response.ContentType = "application/octet-stream"; 56             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); 57             while (dataLengthToRead > 0 && Response.IsClientConnected) 58             { 59                 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小  60                 Response.OutputStream.Write(buffer, 0, lengthRead); 61                 Response.Flush(); 62                 dataLengthToRead = dataLengthToRead - lengthRead; 63             } 64             Response.Close(); 65         } 66     } 67  68 四、//流方式下载  69     protected void Button4_Click(object sender, EventArgs e) 70     { 71         string fileName = "aaa.txt";//客户端保存的文件名  72         string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 73  74         //以字符流的形式下载文件  75         FileStream fs = new FileStream(filePath, FileMode.Open); 76         byte[] bytes = new byte[(int)fs.Length]; 77         fs.Read(bytes, 0, bytes.Length); 78         fs.Close(); 79         Response.ContentType = "application/octet-stream"; 80         //通知浏览器下载文件而不是打开  81         Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 82         Response.BinaryWrite(bytes); 83         Response.Flush(); 84         Response.End(); 85     } 86  87 //---------------------------------------------------------- 88  89 public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody ) 90 { 91  92   WebForm.Response.ClearHeaders(); 93   WebForm.Response.Clear(); 94   WebForm.Response.Expires = 0; 95   WebForm.Response.Buffer = true; 96   WebForm.Response.AddHeader("Accept-Language", "zh-tw"); 97   //'文件名称 98   WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'"); 99   WebForm.Response.ContentType = "Application/octet-stream";100   //'文件内容101   WebForm.Response.Write(FileBody);//-----------102     WebForm.Response.End();103 }104 105 106 //上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:107 108 public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )109 {110   WebForm.Response.ClearHeaders();111   WebForm.Response.Clear();112   WebForm.Response.Expires = 0;113     WebForm.Response.Buffer = true;114   WebForm.Response.AddHeader("Accept-Language", "zh-tw");115   //文件名称116   WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );117   WebForm.Response.ContentType = "Application/octet-stream";118   //文件内容119   WebForm.Response.Write(System.IO.File.Rea}dAllBytes(FilePath));//---------120   WebForm.Response.End();121 122 }
View Code

 

转载于:https://www.cnblogs.com/zhang625161495/p/9351999.html

你可能感兴趣的文章
什么是期望风险、经验风险与结构风险
查看>>
Windows10cmd编码格式改为UTF-8
查看>>
BITMAPFILEHEADER、BITMAPINFOHEADER及BMP结构详解
查看>>
Serilize/DeSerilize tree
查看>>
JIRA4.4.4安装及破解
查看>>
代码性能分析
查看>>
SSM应用(六)--SSM集成
查看>>
poj 2318 判断点在多边形内
查看>>
poj3347线段相交,扩大数据运算
查看>>
jmp && call && ret 特权级转移 & 进程调度
查看>>
python中内建函数isinstance的用法
查看>>
oracle数据库基于(streams流复制)的双机热备配置手册
查看>>
Cheatsheet: 2012 04.25 ~ 05.03
查看>>
java 浮点运算
查看>>
杭电ACM 1000题
查看>>
C# - CSV(Comma-Separated Values)文件读取.
查看>>
使用YUIDoc生成JS文档
查看>>
不成熟的五个特征,你占几个?
查看>>
CentOS 7 服务器配置--安装Java和Tomcat
查看>>
POJ 2823 Sliding Window(优先队列)
查看>>