-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathup.html
77 lines (65 loc) · 2.36 KB
/
up.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en" >
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>TUS Upload Test</title>
</head>
<body>
<script type="text/javascript">
var CREATE_ENDPT = location.protocol + "//" + location.hostname + ":" + location.port + "/files";
function uploadChunk(blobOrFile, submit_url, start, end, total) {
var xhr = new XMLHttpRequest();
xhr.open("PATCH", submit_url, false);
xhr.setRequestHeader('Offset', start);
xhr.setRequestHeader('Content-Type', 'application/offset+octet-stream');
xhr.onload = function(e) {
console.log('uploaded bytes: ' + (end - start) + "/" + total);
};
xhr.send(blobOrFile);
}
function uploadFile(blob) {
var BYTES_PER_CHUNK = 10 * 1024 * 1024; // 10MB chunk sizes.
var SIZE = blob.size;
var xhr = new XMLHttpRequest();
xhr.open("POST", CREATE_ENDPT, false);
xhr.setRequestHeader('Final-Length', SIZE);
xhr.send()
submit_url = xhr.getResponseHeader("Location");
console.log("new post url " + submit_url);
blob.slice = blob.slice || blob.mozSlice || blob.webkitSlice;
var start = 0;
var end = 0;
var startTime = new Date().getTime()/1000;
var speed = 0;
var notes = '';
while(start < SIZE) {
end = start + BYTES_PER_CHUNK;
if (end > SIZE) {
end = SIZE;
}
var chunk = blob.slice(start, end);
uploadChunk(chunk, submit_url, start, end, SIZE);
notes += '<br>uploaded ' + start + "-" + (end - 1) + "/" + SIZE + " chunk size:" + chunk.size;
document.getElementById('notes').innerHTML = notes;
console.log(notes);
start += chunk.size;
}
var elapsedTime = new Date().getTime()/1000 - startTime;
console.log("elapsed Time " + elapsedTime);
speed = (SIZE * 8.0) / elapsedTime;
console.log("speed " + speed);
document.getElementById('speed').innerHTML = speed > 1024 * 1024 ? (speed/(1024 * 1024) + " Mbps") :( speed/1024 + "Kbps");
document.getElementById('t').innerHTML = elapsedTime > 60 ? (elapsedTime/(60) + " mins"): (elapsedTime + " secs");
}
</script>
<h1>TUS Upload Test
<h2> Works with Chrome 27.0.1453.93, Safari 6.04, Firefox 21.0 and IE 10.0.9
<br>
<input type="file" onchange="return uploadFile(this.files[0]);">
<div>
<div id="notes"></div>
<div id="t"></div>
<div id="speed"></div>
</div>
</body>
</html>