[导入]ASP.NET断点续传和多线程下载
在解决etag的问题后,那个.vga文件还是不能正常播放,我只好用最后一招,通过VS.NET断点来监测程序的流程,在我检查Request.Header的时候,忽然发现一项:"Accept-Ranges: bytes0-31",这是什么东东?好像很熟的样子,通过在flashget中一看才知道,原来是断点续传的起始位置和结束位置。我忽然想起我的系统不支持断点续传,而这个再现播放的控件应该是通过断点续传实现的,这应该就是问题所在。
知道问题就好说了,从网上参考了一段代码用来搞断点续传的(Asp.net(c#)实现多线程断点续传)。
看起来确实不错,通过了flashget的测试。
可是我用来测试那个screen.vga,可是依然不能显示,究竟是什么原因呢?
继续用VS.NET断点来监测程序的流程,终于发现问题所在,原来flashget使用Accept-Ranges: bytes0-格式的header来发送请求,也就是说只讲明从什么地方开始而不讲明结束(莫认识文件尾部),而上面的程序也是针对flashget来设计的,却没有考虑到Accept-Ranges: bytes0-31这种有头有尾的格式。
那就容易了,到现在,也该自己写写代码了,不能总靠从网上找现成的,呵呵
以下是整个load.aspx的代码:
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
string queryString=Request.QueryString.GetValues(0)[0];
string fileSpec=Page.Server.MapPath("../resData/"+queryString);
//设置响应时间,以备大文件输出
Page.Server.ScriptTimeout=30000000;
//开始输出文件
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
FileInfo fileInfo=new FileInfo(fileSpec);
try
{//清空已有内容
Response.ClearContent();
Response.ClearHeaders();
//开始检测Range中的断点续传块信息
long startPoint = 0,endPoint=fileInfo.Length-1;
if(Request.Headers["Range"]!=null)
{
Response.StatusCode = 206;
string temp=Request.Headers["Range"].Replace("bytes=","");
if(!temp.StartsWith("-"))
startPoint= long.Parse(temp.Substring(0,temp.IndexOf(''-'')));
if(!temp.EndsWith("-"))
endPoint=long.Parse(temp.Substring(temp.IndexOf(''-'')+1));
}
//返回块信息和文件大小
Response.AppendHeader("Content-Range","bytes " + startPoint.ToString() + "-" +endPoint.ToString() + "/" + fileInfo.Length.ToString());
Response.AppendHeader("Content-Length",((long)(endPoint-startPoint+1)).ToString());
//添加依赖文件,设置etag和LastModified,文件名等头信息
Response.AddFileDependency(fileSpec);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetETagFromFileDependencies();
Response.Cache.SetLastModifiedFromFileDependencies();
Response.AppendHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileInfo.Name)));
Response.ContentType = GetContentType(fileSpec);
//开始输出文件
iStream = new System.IO.FileStream(fileSpec, System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
iStream.Position = startPoint;
dataToRead = endPoint - startPoint+1;
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
if(dataToRead>10000)
length=10000;
else
length=(int)dataToRead;
length = iStream.Read(buffer, 0,length);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
private string GetContentType(string fileSpec)
{
string contentType;
switch (Path.GetExtension(fileSpec))
{
case ".3gp": contentType="audio/AMR-WB-encrypted"; break;
case ".m4a": contentType="audio/m4a"; break;
case ".mid": contentType="audio/midi"; break;
case ".mp1": contentType="audio/mp1"; break;
case ".mp2": contentType="audio/mp2"; break;
case ".mp3": contentType="audio/mpeg"; break;
case ".m3u": contentType="audio/mpegurl"; break;
case ".mpga": contentType="audio/rn-mpeg"; break;
case ".pls": contentType="audio/scpls"; break;
case ".ra": contentType="audio/vnd.rn-realaudio"; break;
case ".wav": contentType="audio/wav"; break;
case ".aiff": contentType="audio/x-aiff"; break;
case ".wax": contentType="audio/x-ms-wax"; break;
case ".wma": contentType="audio/x-ms-wma"; break;
case ".aif": contentType="audio/x-pn-aiff"; break;
case ".au": contentType="audio/x-pn-au"; break;
case ".ram": contentType="audio/x-pn-realaudio"; break;
case ".rms": contentType="audio/x-realaudio-secure"; break;
case ".bmp": contentType="image/bmp"; break;
case ".gif": contentType="image/gif"; break;
case ".jpg": contentType="image/jpeg"; break;
case ".png": contentType="image/png"; break;
case ".tif": contentType="image/tiff"; break;
case ".rp": contentType="image/vnd.rn-realpix"; break;
case ".ico": contentType="image/x-icon"; break;
case ".xbm": contentType="image/x-xbitmap"; break;
case ".css": contentType="text/css"; break;
case ".htm": contentType="text/html"; break;
case ".uls": contentType="text/iuls"; break;
case ".txt": contentType="text/plain"; break;
case ".wsc": contentType="text/scriptlet"; break;
case ".rt": contentType="text/vnd.rn-realtext"; break;
case ".htt": contentType="text/webviewhtml"; break;
case ".htc": contentType="text/x-component"; break;
case ".vcf": contentType="text/x-vcard"; break;
case ".xml": contentType="text/xml"; break;
case ".3g2": contentType="video/3gpp2"; break;
case ".mpeg": contentType="video/mpeg"; break;
case ".avi": contentType="video/msvideo"; break;
case ".rv": contentType="video/vnd.rn-realvideo"; break;
case ".mpa": contentType="video/x-mpg"; break;
case ".asx": contentType="video/x-ms-asf"; break;
case ".asf": contentType="video/x-ms-asf"; break;
case ".wm": contentType="video/x-ms-wm"; break;
case ".wmv": contentType="video/x-ms-wmv"; break;
case ".wmx": contentType="video/x-ms-wmx"; break;
case ".vga": contentType="PowerCreator VGAPlayer.Document"; break;
default: contentType="Application/"+Path.GetExtension(fileSpec);break;
}
return contentType;
}
posted on 2005-03-21 17:13 K_Reverter 阅读(1230) 评论(1) 编辑 收藏 举报