Asp.net resumable upload

Source: Internet
Author: User
Resumable upload. The disadvantage is that the file name must be manually managed and cannot be repeated.
<%
Url = "http: // 127.0.0.1/test/20021317173156951.swf" 'address for testing
If url = "" Then die ("URL cannot be blank.") 'dare to repeat me, empty URL can't'
T = faster Rev (URL, "/") 'Get the last "/" location'
If T = 0 or T = Len (URL) Then die ("cannot get file name.") 'No "/" or end'
Filename = right (URL, Len (URL)-T) 'get the file name to be saved'
If not left (URL, 7) = "http: //" then url = "http: //" & url ", if you forget" http: // "carelessly, add'
Filename = server. mappath (filename)
Set FSO = server. Createobject ("scripting. FileSystemObject") 'fso, Aso, and HTTP. Each object must be unique'
Set Aso = server. Createobject ("ADODB. Stream ")
Set HTTP = server. Createobject ("Microsoft. XMLHTTP ")

If FSO. fileexists (filename) Then 'determine whether the object to be downloaded already exists'
Start = FSO. GetFile (filename). Size 'exists, with the current file size as the starting position'
Else
Start = 0 'nonexistent, everything starts from scratch'
FSO. createtextfile (filename). Close 'create file'
End if

Response. Write "connectting ..."
Response. Flush 'first play'
Current = start' current position is the start position
Do
HTTP. Open "get", URL, and false' HTTP is called in synchronous mode. I wanted to use asynchronous mode, but it has never been successful.
HTTP. setRequestHeader "range", "bytes =" & start & "-" & CSTR (start + 20480) 'here is the secret of resumable upload.
HTTP. setRequestHeader "Content-Type:", "application/octet-stream"
HTTP. Send' start sending after data packets are constructed'
For I = 1 to 60 'cyclic waiting'
If HTTP. readystate = 3 then showplan () 'status 3 indicates that the data is received and the progress is displayed'
If HTTP. readystate = 4 then exit for 'status 4 indicates the data is accepted successfully'
Sleep (1) 'Here is the latency. The square method is not very good.
Next
If not HTTP. readystate = 4 then die ("time-out.") '20 K is not finished in 1 minute? Timeout! '
If HTTP. Status> 299 then die ("error:" & HTTP. Status & "& HTTP. statustext) 'Isn't it, and an error occurs again? '
If not HTTP. Status = 206 then die ("the server does not support resumable upload.") 'the server does not support resumable upload'

Aso. type = 1 'data stream 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'
Aso. Close

Range = http. getResponseHeader ("content-range") 'Get "content-range"' in the HTTP Header "'
If range = "" Then die ("no.") 'You don't know if the download is complete'
Temp = mid (range, instr (range, "-") + 1) 'content-range is similar to 123-456/789'
Current = clng (left (temp, instr (temp, "/")-1) '2014 is the start position and 123 is the end position'
Total = clng (mid (temp, instr (temp, "/") + 1) '100 is the total number of bytes of the file'
If total-current = 1 then exit do 'the end position is 1 less than the total size, indicating that the transfer is completed'
Start = start + 20480 'otherwise download 20 k'
Loop while true

Response. Write CHR (13) & "downloaded (" & total & ")." 'downloaded, total Byte Count'

Function die (MSG) 'function name comes from Perl built-in function die'
Response. Write MSG 'last words ^_^'
Response. End 'Go to see Marx'
End Function

Function showplan () 'display 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 (13) & "download (" ¤ T & ")" & C & CHR (8) '13' ASCII code is returned to the beginning of the line, and 8'
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 and minute of the current time into seconds and stores them in CT
Ct = t1 + T2 + T3
// Loop wait
Do
T1 = hour (time) * 3600
T2 = minute (time) * 60
T3 = second (time)
Lt = t1 + T2 + T3
Loop while (LT-CT <delaytime)
End sub
%>

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.