Using XMLHttpRequest technology to implement breakpoint renewal download file

Source: Internet
Author: User
Tags rar

1, the code you have used (no breakpoint continuation code, the breakpoint continued to refer to the following 2nd part, I can achieve):

<HTML>
<HEAD>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>downloadfile servlet</title>
<script language= "JavaScript" >
Download files

Strremoteurl: Remote file to download, request is true URL or file stream
Strlocalurl: Local file path to store
function DownloadFile (Strremoteurl,strlocalurl)
{
try {
var XmlHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
Xmlhttp.open ("Get", strremoteurl,false);
Xmlhttp.send ();
var adodbstream = new ActiveXObject ("ADODB.stream");
Adodbstream.type = 1;//1=adtypebinary
Adodbstream.open ();
Adodbstream.write (Xmlhttp.responsebody);
Adodbstream.savetofile (strlocalurl,2);
Adodbstream.close ();
Adodbstream = null;
XmlHTTP = null;
return true;
//}
catch (e) {
Alert ("Download error!");
return false;
//}
}
</script>
</HEAD>
<BODY>
<form name= "frm" method= "post" >
<input type= "hidden" name= "returnvalue" value= ""/>
<input type= "button" name= "downloadbtn value=" Download ... "onclick=" javascript:downloadfile (' http:// 128.128.96.28:8001/app/ibscontentservletfv5?filename=0721wlpt_app.rar&branchid=000000000&language=cn &ibsreqtype=2 ', ' D:/0721.rar ') >
</form>
</BODY>
</HTML>

2,asp Breakpoint Continuous transmission

Free to do nothing, got an ASP continued to pass the program, is not very satisfied, when the brick, there are jade in spite of smashing over it
<%
Url= "http://127.0.0.1/test/20051117173156951.swf" ' Test address
If Url= "" Then die ("url cannot be empty.") "Dare to scare me, empty URL can not do"
T=instrrev (URL, "/") ' Get last '/' position '
If t=0 or T=len (URL) then die ("not getting filename.") ' No '/' or '/' end '
Filename=right (Url,len (URL)-T) ' gets the filename to save '
If not left (url,7) = "http://" then url= "http://" if Careless "&url" forget, plus '
Filename=server.mappath (filename)
Set Fso=server.createobject ("Scripting.FileSystemObject") ' fso,aso,http three objects can be less than one '
Set Aso=server.createobject ("ADODB. Stream ")
Set Http=server.createobject ("Microsoft.XMLHTTP")

If fso.fileexists (filename) Then ' Determine if the file being downloaded already exists '
Start=fso.getfile (filename). Size ' exists with the current file size as the starting position '
Else
Start=0 ' not exist, everything starts from scratch '
Fso.createtextfile (filename). Close ' New file '
End If

Response.Write "Connectting ..."
Response.Flush ' The show just started '
Current=start ' current position is the starting position
Todo
Http.open "Get", Url,false ' here to call HTTP in a synchronous way, would have been unsuccessful in an asynchronous way
Http.setrequestheader "Range", "bytes=" &start& "-" &cstr (start+20480) "The mystery of the continuation of the breakpoint is right here.
Http.setrequestheader "Content-type:", "Application/octet-stream"
Http.send ' constructs the packet and starts sending '
For I=1 to 60 ' Loop wait '
If Http.readystate=3 then Showplan () ' State 3 means start receiving data, show progress '
If http.readystate=4 then exit for ' state 4 indicates data acceptance completed '
Sleep (1) ' Here is the delay, the positive method is not very good.
Next
If not http.readystate=4 then die ("timeout.") ' 1 minutes has not finished 20k. Timeout. '
If http.status>299 then die ("Error:" &http.status& "&http.statustext)" No, it's wrong again. '
If not http.status=206 then die ("Server does not support breakpoint continuation.") ' Server does not support breakpoint continuation '

Aso.type=1 ' data flow type set to Byte '
Aso.open
Aso.loadfromfile filename ' Open file '
Aso.position=start ' Set the initial position of the file pointer '
Aso.write http.responsebody ' Write Data '
Aso.savetofile filename,2 ' overwrite save '
Aso.close

Range=http.getresponseheader ("Content-range") ' Gets the "content-range" in the HTTP header
If range= "" Then Die ("not get.") ' Without it, I don't know if I've finished downloading '
Temp=mid (RANGE,INSTR (range, "-") +1) ' Content-range is similar to 123-456/789 '
CURRENT=CLNG (Left (temp,instr (temp, "/")-1)) ' 123 is the starting position, 456 is the end position '
TOTAL=CLNG Mid (Temp,instr (temp, "/") +1)) ' 789 is the total number of files bytes '
If Total-current=1 then exit Do ' end position 1 less than total size means the transfer is complete '
start=start+20480 ' Otherwise download 20k '
Loop while True

Response.Write Chr (&) has been downloaded ("&total&"). " ' Download finished, show total bytes '

Function Die (msg) ' Functions name from Perl built-in functions die '
Response.Write msg ' confessed ^_^ '
Response.End ' to see Max '
End Function

Function showplan () ' Show Download Progress '
If I mod 3 = 0 Then c= "/" ' simple dynamic effect '
If I mod 3 = 1 then c= "-"
If I mod 3 = 2 then c= "/"
Response.Write Chr & "Download (" &current& ")" &C&CHR (8) ' 13th "ASCII code is back to the beginning, 8th is backspace '
Response.Flush
End Function


Sub Sleep (delaytime)
Dim t1,t2,t3,ct,lt
T1=hour (Time) *3600
T2=minute (Time) *60
T3=second (Time)
Converts the hour of the current time, minutes into seconds, into the CT
Ct=t1+t2+t3
Loop wait
Todo
T1=hour (Time) *3600
T2=minute (Time) *60
T3=second (Time)
Lt=t1+t2+t3
Loop while (Lt-ct<delaytime)
End Sub
%>


I really admire this guy's comments: Editor's note.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.