[导入]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的代码:

 

None.gif <% @ import Namespace = " System.IO "   %>
None.gif
< script runat = " server " >
None.gif    
private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif 
string queryString=Request.QueryString.GetValues(0)[0];
InBlock.gif 
string fileSpec=Page.Server.MapPath("../resData/"+queryString);
InBlock.gif
//设置响应时间,以备大文件输出
InBlock.gif
        Page.Server.ScriptTimeout=30000000;
InBlock.gif 
//开始输出文件
InBlock.gif
 System.IO.Stream iStream = null;
InBlock.gif 
byte[] buffer = new Byte[10000];
InBlock.gif 
int length;
InBlock.gif 
long dataToRead;
InBlock.gif FileInfo fileInfo
=new FileInfo(fileSpec);
InBlock.gif
InBlock.gif 
try
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{//清空已有内容
InBlock.gif
  Response.ClearContent();
InBlock.gif  Response.ClearHeaders();
InBlock.gif
//开始检测Range中的断点续传块信息
InBlock.gif
  long startPoint = 0,endPoint=fileInfo.Length-1;
InBlock.gif  
if(Request.Headers["Range"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   Response.StatusCode 
= 206;
InBlock.gif   
string temp=Request.Headers["Range"].Replace("bytes=","");
InBlock.gif   
if(!temp.StartsWith("-"))
InBlock.gif    startPoint
= long.Parse(temp.Substring(0,temp.IndexOf(''-'')));
InBlock.gif   
if(!temp.EndsWith("-"))
InBlock.gif    endPoint
=long.Parse(temp.Substring(temp.IndexOf(''-'')+1));
ExpandedSubBlockEnd.gif  }

InBlock.gif
//返回块信息和文件大小
InBlock.gif
  Response.AppendHeader("Content-Range","bytes " + startPoint.ToString() + "-" +endPoint.ToString() + "/" + fileInfo.Length.ToString());   
InBlock.gif  Response.AppendHeader(
"Content-Length",((long)(endPoint-startPoint+1)).ToString());
InBlock.gif
//添加依赖文件,设置etag和LastModified,文件名等头信息
InBlock.gif
  Response.AddFileDependency(fileSpec);
InBlock.gif  Response.Cache.SetCacheability(HttpCacheability.Public);
InBlock.gif  Response.Cache.SetETagFromFileDependencies();
InBlock.gif  Response.Cache.SetLastModifiedFromFileDependencies();
InBlock.gif  Response.AppendHeader(
"Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(fileInfo.Name)));
InBlock.gif  Response.ContentType 
= GetContentType(fileSpec);
InBlock.gif
InBlock.gif
//开始输出文件
InBlock.gif
  iStream = new System.IO.FileStream(fileSpec, System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
InBlock.gif  iStream.Position 
= startPoint;
InBlock.gif  dataToRead 
= endPoint - startPoint+1;
InBlock.gif  
while (dataToRead > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if (Response.IsClientConnected) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(dataToRead>10000)
InBlock.gif     length
=10000;
InBlock.gif    
else
InBlock.gif     length
=(int)dataToRead;
InBlock.gif    length 
= iStream.Read(buffer, 0,length);
InBlock.gif    Response.OutputStream.Write(buffer, 
0, length);
InBlock.gif    Response.Flush();
InBlock.gif    buffer
= new Byte[10000];
InBlock.gif    dataToRead 
= dataToRead - length;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dataToRead 
= -1;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
catch (Exception ex) 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  Response.Write(
"Error : " + ex.Message);
ExpandedSubBlockEnd.gif }

InBlock.gif 
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
if (iStream != null
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   iStream.Close();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif    
private   string  GetContentType( string  fileSpec)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif 
string contentType;
InBlock.gif 
switch (Path.GetExtension(fileSpec))
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif
case ".3gp": contentType="audio/AMR-WB-encrypted"break;
InBlock.gif
case ".m4a": contentType="audio/m4a";  break;
InBlock.gif
case ".mid": contentType="audio/midi";  break;
InBlock.gif
case ".mp1": contentType="audio/mp1";  break;
InBlock.gif
case ".mp2": contentType="audio/mp2";  break;
InBlock.gif
case ".mp3": contentType="audio/mpeg";  break;
InBlock.gif
case ".m3u": contentType="audio/mpegurl";  break;
InBlock.gif
case ".mpga": contentType="audio/rn-mpeg";  break;
InBlock.gif
case ".pls": contentType="audio/scpls";  break;
InBlock.gif
case ".ra": contentType="audio/vnd.rn-realaudio"break;
InBlock.gif
case ".wav": contentType="audio/wav";  break;
InBlock.gif
case ".aiff": contentType="audio/x-aiff";  break;
InBlock.gif
case ".wax": contentType="audio/x-ms-wax";  break;
InBlock.gif
case ".wma": contentType="audio/x-ms-wma";  break;
InBlock.gif
case ".aif": contentType="audio/x-pn-aiff";  break;
InBlock.gif
case ".au": contentType="audio/x-pn-au";  break;
InBlock.gif
case ".ram": contentType="audio/x-pn-realaudio"break;
InBlock.gif
case ".rms": contentType="audio/x-realaudio-secure"break;
InBlock.gif
case ".bmp": contentType="image/bmp";  break;
InBlock.gif
case ".gif": contentType="image/gif";  break;
InBlock.gif
case ".jpg": contentType="image/jpeg";  break;
InBlock.gif
case ".png": contentType="image/png";  break;
InBlock.gif
case ".tif": contentType="image/tiff";  break;
InBlock.gif
case ".rp": contentType="image/vnd.rn-realpix"break;
InBlock.gif
case ".ico": contentType="image/x-icon";  break;
InBlock.gif
case ".xbm": contentType="image/x-xbitmap";  break;
InBlock.gif
case ".css": contentType="text/css";   break;
InBlock.gif
case ".htm": contentType="text/html";  break;
InBlock.gif
case ".uls": contentType="text/iuls";  break;
InBlock.gif
case ".txt": contentType="text/plain";  break;
InBlock.gif
case ".wsc": contentType="text/scriptlet";  break;
InBlock.gif
case ".rt": contentType="text/vnd.rn-realtext"break;
InBlock.gif
case ".htt": contentType="text/webviewhtml";  break;
InBlock.gif
case ".htc": contentType="text/x-component";  break;
InBlock.gif
case ".vcf": contentType="text/x-vcard";  break;
InBlock.gif
case ".xml": contentType="text/xml";   break;
InBlock.gif
case ".3g2": contentType="video/3gpp2";  break;
InBlock.gif
case ".mpeg": contentType="video/mpeg";  break;
InBlock.gif
case ".avi": contentType="video/msvideo";  break;
InBlock.gif
case ".rv": contentType="video/vnd.rn-realvideo"break;
InBlock.gif
case ".mpa": contentType="video/x-mpg";  break;
InBlock.gif
case ".asx": contentType="video/x-ms-asf";  break;
InBlock.gif
case ".asf": contentType="video/x-ms-asf";  break;
InBlock.gif
case ".wm": contentType="video/x-ms-wm";  break;
InBlock.gif
case ".wmv": contentType="video/x-ms-wmv";  break;
InBlock.gif
case ".wmx": contentType="video/x-ms-wmx";  break;
InBlock.gif
case ".vga": contentType="PowerCreator VGAPlayer.Document"break;
InBlock.gif
default: contentType="Application/"+Path.GetExtension(fileSpec);break;
ExpandedSubBlockEnd.gif }

InBlock.gif    
return contentType;
ExpandedBlockEnd.gif    }

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值