-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.js
52 lines (41 loc) · 1.6 KB
/
uploader.js
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
var handleUpload = function(ev){
ev.preventDefault();
ev.stopPropagation();
var fileInput = document.getElementById('file');
var data = new FormData();
data.append('ajax',true);
for (var i = 0;i<fileInput.files.length;++i){
data.append('file[]',fileInput.files[i]);
}
var request = new XMLHttpRequest();
request.upload.addEventListener('progress',function(ev){
if (ev.lengthComputable){
var percent = ev.loaded / ev.total;
document.getElementById("progress").setAttribute('style','width:'+Math.round(percent * 100)+'%');
document.getElementById("progress").innerHTML = Math.round(percent * 100)+'%';
}
});
request.upload.addEventListener('load',function(ev){
document.getElementById('upload_progress').style.display = "none";
});
request.upload.addEventListener('error',function(ev){
alert('Uplaod failed');
});
request.addEventListener('readystatechange',function(ev){
if(this.readyState == 4 ){
if (this.status == 200){
document.getElementById("progress").setAttribute('style','width:0%');
}else{
console.log('Error: ' + this.status);
}
}
})
request.open('POST','index.php');
request.setRequestHeader('Cache-Control', 'no-cache');
document.getElementById('upload_progress').style.display = "block";
request.send(data);
}
window.addEventListener('load',function(ev){
var submit = document.getElementById('submit');
submit.addEventListener('click',handleUpload);
})