zsx's Old Blog

黑历史逐步隐藏中_(:з」∠)_仅保留部分可能有些价值的文章,并不保证将来的可访问性。

ASP XMLHTTP断点续传+进度显示

代码:

Dim objPing,strMax,i
Set objPing = Server.CreateObject("MSXML2.ServerXMLHTTP")
objPing.open "HEAD", "地址",False
objPing.send 
strMax=CDBl(objPing.getResponseHeader("Content-Length"))  
'以上代码通过HEAD请求来得到字节数,以便断点续传
  
Response.Write "大小:" & FormatNumber(strMax/1024/1024,"3.33") & "MB, 下载中.."
Response.Flush()
  
  
Dim MyStream,s
Set MyStream=Server.CreateObject("Adodb.Stream") 
MyStream.Type = 1
MyStream.Mode = 3
MyStream.Open 
  
  
  
For i=-1 To strMax Step 1000000 '1000000是每次下载字节数
    s=IIf(i+1000000>strMax,strMax,i+1000000)
    objPing.open "GET", "地址",False
    objPing.setRequestHeader "Range","bytes="&i+1&"-"&s
              
    'HTTP 1.1的断点续传,就通过Range来实现
              
    objPing.send 
    MyStream.Write objPing.responsebody 
    Response.Write "<p>已下载:" & CInt(s/strMax*100) & "% </p>"
    Response.Flush()
Next
  
MyStream.SaveToFile Server.MapPath(".") & "\" & "文件名" ,2
'当然你也可以一边下载一边保存


控制面板
您好,欢迎到访网站!
  查看权限
Google Adsense
文章归档
站点信息
  • 文章总数:259
  • 页面总数:10
  • 分类总数:17
  • 标签总数:136
  • 评论总数:1644
  • 浏览总数:216659

Powered By Z-BlogPHP 1.5.1 Zero

闽ICP备15006942号   闽公网安备 35010302000147号