-
Notifications
You must be signed in to change notification settings - Fork 36
/
sketch.js
49 lines (41 loc) · 1.44 KB
/
sketch.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
// based on http://www.html5rocks.com/en/tutorials/file/dndfiles/
// When we get text we'll just make a paragraph element with the text
function process(text) {
createP(text);
}
function setup() {
noCanvas();
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
console.log('Great success! All the File APIs are supported');
} else {
alert('The File APIs are not fully supported in this browser.');
}
//<input type="file" id="files" name="files[]" multiple />
// Make the file input
var fileInput = createInput();
// Set attribute to file
fileInput.attribute('type','file');
// If we want to allow multiple files
fileInput.attribute('multiple','');
// If a file is selected this event will be triggered
fileInput.elt.addEventListener('change', handleFileSelect, false);
// For the list of files
var list = createElement('ol','');
// Function to handle when a file is selected
function handleFileSelect(evt) {
// A FileList
var files = evt.target.files;
// Show some properties
for (var i = 0, f; f = files[i]; i++) {
var file = createElement('li',f.name + ' ' + f.type + ' ' + f.size + ' bytes');
file.parent(list);
// Read the file and process the result
var reader = new FileReader();
reader.readAsText(f);
reader.onload = function(e) {
process(e.target.result);
}
}
}
}