文件下载的四种方式 itprobie

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 
  6 using System.IO;
  7 
  8 /// <summary>
  9 /// 文件下载有以下四种方式, 大文件下载的处理方法:将文件分块下载。
 10 /// Response.OutputStream.Write
 11 /// Response.TransmitFile
 12 /// Response.WriteFile
 13 /// Response.BinaryWrite
 14 /// </summary>
 15 public class DownHelper
 16 {
 17     HttpResponse Response = null;
 18     public DownHelper()
 19     {
 20         Response = HttpContext.Current.Response;
 21     }
 22 
 23     public void DownloadByOutputStreamBlock(System.IO.Stream stream, string fileName)
 24     {
 25         using (stream)
 26         {
 27             //将流的位置设置到开始位置。
 28             stream.Position = 0;
 29             //块大小
 30             long ChunkSize = 102400;
 31             //建立100k的缓冲区
 32             byte[] buffer = new byte[ChunkSize];
 33             //已读字节数
 34             long dataLengthToRead = stream.Length;
 35 
 36             Response.ContentType = "application/octet-stream";
 37             Response.AddHeader("Content-Disposition",
 38                 string.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(fileName)));
 39 
 40             while (dataLengthToRead > 0 && Response.IsClientConnected)
 41             {
 42                 int lengthRead = stream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
 43                 Response.OutputStream.Write(buffer, 0, lengthRead);
 44                 Response.Flush();
 45                 Response.Clear();
 46                 dataLengthToRead -= lengthRead;
 47             }
 48             Response.Close();
 49         }
 50     }
 51 
 52     public void DownloadByTransmitFile(string filePath, string fielName)
 53     {
 54         FileInfo info = new FileInfo(filePath);
 55         long fileSize = info.Length;
 56         Response.Clear();
 57         Response.ContentType = "application/x-zip-compressed";
 58         Response.AddHeader("Content-Disposition",
 59             string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fielName)));
 60         //不指明Content-Length用Flush的话不会显示下载进度  
 61         Response.AddHeader("Content-Length", fileSize.ToString());
 62         Response.TransmitFile(filePath, 0, fileSize);
 63         Response.Flush();
 64         Response.Close();
 65     }
 66 
 67     public void DownloadByWriteFile(string filePath, string fileName)
 68     {
 69         FileInfo info = new FileInfo(filePath);
 70         long fileSize = info.Length;
 71         Response.Clear();
 72         Response.ContentType = "application/octet-stream";
 73         Response.AddHeader("Content-Disposition",
 74             string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
 75 
 76         //指定文件大小  
 77         Response.AddHeader("Content-Length", fileSize.ToString());
 78         Response.WriteFile(filePath, 0, fileSize);
 79         Response.Flush();
 80         Response.Close();
 81     }
 82 
 83     public void DownloadByOutputStreamBlock(string filePath, string fileName)
 84     {
 85         using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
 86         {
 87             //指定块大小  
 88             long chunkSize = 102400;
 89             //建立一个100K的缓冲区  
 90             byte[] buffer = new byte[chunkSize];
 91             //已读的字节数  
 92             long dataToRead = stream.Length;
 93 
 94             //添加Http头  
 95             Response.ContentType = "application/octet-stream";
 96             Response.AddHeader("Content-Disposition",
 97                 string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
 98             Response.AddHeader("Content-Length", dataToRead.ToString());
 99 
100             while (dataToRead > 0 && Response.IsClientConnected)
101             {
102                 int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
103                 Response.OutputStream.Write(buffer, 0, length);
104                 Response.Flush();
105                 Response.Clear();
106                 dataToRead -= length;
107             }
108             Response.Close();
109         }
110     }
111 
112     public void DownloadByBinary(string filePath, string fileName)
113     {
114         using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
115         {
116             //指定块大小  
117             long chunkSize = 102400;
118             //建立一个100K的缓冲区  
119             byte[] bytes = new byte[chunkSize];
120             //已读的字节数  
121             long dataToRead = stream.Length;
122 
123             //添加Http头  
124             Response.ContentType = "application/octet-stream";
125             Response.AddHeader("Content-Disposition",
126                 string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
127 
128             Response.AddHeader("Content-Length", bytes.Length.ToString());
129             Response.BinaryWrite(bytes);
130             Response.Flush();
131             Response.Close();
132         }
133     }
134 
135     public void DownloadByBinaryBlock(string filePath, string fileName)
136     {
137         using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
138         {
139             //指定块大小  
140             long chunkSize = 102400;
141             //建立一个100K的缓冲区  
142             byte[] buffer = new byte[chunkSize];
143             //已读的字节数  
144             long dataToRead = stream.Length;
145 
146             //添加Http头  
147             Response.ContentType = "application/octet-stream";
148             Response.AddHeader("Content-Disposition",
149                 string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
150             Response.AddHeader("Content-Length", dataToRead.ToString());
151 
152             while (dataToRead > 0 && Response.IsClientConnected)
153             {
154                 int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
155                 Response.BinaryWrite(buffer);
156                 Response.Flush();
157                 Response.Clear();
158 
159                 dataToRead -= length;
160             }
161             Response.Close();
162         }
163     }
164 }
原文地址:https://www.cnblogs.com/guohu/p/2691063.html