-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.html
83 lines (78 loc) · 2.64 KB
/
fileupload.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
77
78
79
80
81
82
83
<!DOCTYPE html>
<html>
<head>
<title>Fast Upload</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
#drop_zone { border: 10px dashed #ccc; width: 100px; min-height: 100px; margin: 20px auto;}
#drop_zone.hover { border: 10px dashed #0c0; }
#drop_zone img { display: block; margin: 10px auto; }
#drop_zone p { margin: 10px; font-size: 14px; }
</style>
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<div id="drop_zone">
Drop files here
</div>
<output id="list"></output>
<script>
Element.prototype.remove = function() {
this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
for(var i = 0, len = this.length; i < len; i++) {
if(this[i] && this[i].parentElement) {
this[i].parentElement.removeChild(this[i]);
}
}
}
var worker = new Worker('fileupload.js');
worker.onmessage = function(e) {
var li = document.getElementById(escape(e.data.data));
if (li)
li.remove();
else
alert(e.data.data);
}
worker.onerror =werror;
function werror(e) {
console.log('ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message);
}
function handleFileSelect(evt) {
//evt.stopPropagation();
evt.preventDefault();
var files = evt.target.files || evt.dataTransfer.files;
// FileList object.
try {
worker.postMessage({
'files' : files
});
} catch(e) {
alert('Can\'t spawn files to worker - '+e)
return
}
//Sending File list to worker
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li id="',escape(f.name),'"><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', '</li>');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
function handleDragOver(evt) {
//evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
// Explicitly show this is a copy.
return false;
}
// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
</html>