I am trying to do something like this:
JavaScript:
function readMultipleFiles(evt) {
//Retrieve all the files from the FileList object
var files = evt.target.files;
window.array = []
if (files) {
for (var i = 0, f; f = files[i]; i++) {
var r = new FileReader();
r.onload = (function (f) {
return function (e) {
var contents = e.target.result;
window.array.push(contents);
};
})(f);
r.readAsText(f);
}
alert(window.array);
} else {
alert("Failed to load files");
}
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
HTML:
<input type="file" id="fileinput" multiple />
So that I can read local files locally.
However, instead I want to store the file contents in an array of this format:
[file1Contents, file2Contents, file3Contents... etc]
How would I go about this? I tried setting a global variable (e.g. window.fileContents) and updating inside the on.load, but that did not work
Thanks
I’m not exactly sure what you’re looking to do, but I’ll give it a swing. I see that you’re already reading the file as text.. could you not just shove that into the array there? I’ve modified your code below: