C#实现文件分块下载
C#实现的文件分块下载,分块输出缓存内容下载到客户端源代码
+展开
-C#
//下载文件的路径
string path = Server.MapPath("广告.jpg");
//下载文件的名称
string filename = "广告.jpg";
System.IO.FileInfo toDownload = new System.IO.FileInfo(path);
if (toDownload.Exists == true)
{
const long ChunkSize = 10000;
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(path);
long dataLengthToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=new_" + HttpUtility.UrlEncode(toDownload.Name));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
string path = Server.MapPath("广告.jpg");
//下载文件的名称
string filename = "广告.jpg";
System.IO.FileInfo toDownload = new System.IO.FileInfo(path);
if (toDownload.Exists == true)
{
const long ChunkSize = 10000;
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(path);
long dataLengthToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=new_" + HttpUtility.UrlEncode(toDownload.Name));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
加支付宝好友偷能量挖...