Get Filename After Filereader Asynchronously Loaded A File
I am loading several files in a directory in order to parse some data from them. This works great so far, but I would like to know which file I am looking at. So I need the name of
Solution 1:
Create a closure around the File
to capture the current file. Then you can get the filename.
An example: http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files
Closure to capture the file information.
functionparseData(entries){
for (var i=0; i<entries.length; i++) {
reader.onloadend = (function(file) {
returnfunction(evt) {
createListItem(evt, file)
};
})(entries[i]);
reader.readAsText(entries[i]);
}
}
And the called function gets an additional argument
functioncreateListItem(evt, file) {
console.log(evt.target.result)
console.log(file.name);
}
Solution 2:
The following source code add an attribute to the file reader
for(i=0; i < files.length; i++)
{
var fileReader = newFileReader();
fileReader.onload = function(file)
{
// DO what you need here// file name = file.target.fileName
} // end of reader load
fileReader.fileName = files[i].name;
fileReader.readAsBinaryString(files[i]);
}
Post a Comment for "Get Filename After Filereader Asynchronously Loaded A File"