当前位置:编程学习 > asp >>

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
'当然你也可以一边下载一边保存
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,