asp.net c# 断点续传 下载 Accept-Ranges

时间:2022-10-26 19:25:29

转自:http://www.cnblogs.com/90nice/p/3489287.html

1.因为要下载大文件 需要断点续传,使用多线程 分段下载 效率比较高,节省资源。

发点牢骚:下载可以用多线程,如果是上传 我觉得没必要了。如果是普通用户(adsl) 上传速度只有50KB/s  据我所知100MB光纤 家庭用户下载理论值能达到12.5MB/S 但是上传目前却只有 256kb/s   这点速度够干啥的。希望以后 上传下载速度能一致。

下面是一个简单的例子:

服务端:

如果是分段下载 在http Header 头文件里 要有Range ,下面会有 客户端的例子。

var range = Request.Headers["Range"];
if (null == range)
{ //正常情况 200
Response.StatusCode = ;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
FileStream fs = new FileStream(fUrl, FileMode.Open);
long len = fs.Length;
fs.Close();
fs.Dispose();
Response.AddHeader("Accept-Ranges", len.ToString());
Response.TransmitFile(fUrl);
}
else
{ //Accept - Range 情况
string[] re = range.Split('=');
string[] r = re[].Split('-');
long start = ;
long alength = ;
long fslength = ;
// 如果开始为空 从0开始取
if (string.IsNullOrEmpty(r[]))
start = ;
else
start = Convert.ToInt64(r[]);
//取文件总长度
FileStream fs = new FileStream(fUrl, FileMode.Open);
fslength = fs.Length;
fs.Close();
fs.Dispose();
// 字节长度
if (string.IsNullOrEmpty(r[]))
alength = fslength - start;
else
alength = Convert.ToInt64(r[]) - start;
Response.StatusCode = ; //分段下载状态
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
Response.AddHeader("Accept-Ranges", (alength - start).ToString());
Response.AddHeader("Content-Range", "bytes " + start + "-" + (start + alength) + "/" + fslength.ToString()); //Content-Range: bytes 100-200/1024 告诉客户端 本次请求的文件位置和总文件大小
// 文件路径 开始位置 取多少字节
Response.TransmitFile(fUrl, start, alength);

客户端 例子:

创建一个 http请求 并 写入请求的文件段

var request = (HttpWebRequest)WebRequest.Create("http://localhost:40337/PublicPage/ZrqPublicImg.zip");
request.AddRange(, );
try
{
//获取HTTP回应,注意HttpWebResponse继承自IDisposable
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
throw new Exception("文件不支持Range部分下载");
//设置接收信息的缓冲器
var bytes = new byte[];
//获取回应的Stream(字节流)
using (var stream = response.GetResponseStream())
{//FileMode.Append 我这里是从文件末尾处写入 如果是多线程 就要根据具体的请求的位置 写入文件
using (var outStream = new FileStream(@"D:\222.jpg", FileMode.Append, FileAccess.Write, FileShare.None))
{
const int bufferLen = ;
byte[] buffer = new byte[bufferLen];
int count = ; while ((count = stream.Read(buffer, , bufferLen)) > )
{
outStream.Write(buffer, , count);
}
outStream.Close();
stream.Close();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("错误信息:{0}", ex.Message);
}

以下摘自:http://www.sufeinet.com/forum.php?mod=viewthread&tid=2258&extra=page%3D1%26filter%3Dtypeid%26typeid%3D284%26typeid%3D284

另附支持限速下载的方法(服务器代码):

/// <summary>
/// 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
/// </summary>
/// <param name="_Request">;Page.Request对象</param>
/// <param name="_Response">;Page.Response对象</param>
/// <param name="_fileName">下载文件名</param>
/// <param name="_fullPath">带文件名下载路径</param>
/// <param name="_speed">每秒允许下载的字节数</param>
/// <returns>返回是否成功</returns>
//---------------------------------------------------------------------
//调用:
// string FullPath=Server.MapPath("count.txt");
// ResponseFile(this.Request,this.Response,"count.txt",FullPath,100);
//---------------------------------------------------------------------
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false; long fileLength = myFile.Length;
long startBytes = ;
int pack = ; //10K bytes
int sleep = (int)Math.Floor((double)( * pack / _speed)) + ; if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = ;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != )
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - , fileLength));
} _Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + ; for (int i = ; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
}