I can't manage to get both the result of the filereader and some parameters in a onload function. This is my code:
HTML of control:
<input type="file" id="files_input" multiple/>
Javascript function:
function openFiles(evt){
var files = evt.target.files;
for (var i = 0; i < files.length; i++) {
var file=files[i];
reader = new FileReader();
reader.onload = function(){
var data = $.csv.toArrays(this.result,{separator:'\t'});
};
reader.readAsText(file);
}
}
Add event:
files_input.addEventListener("change", openFiles, false);
I use the filereader.result
, in the onload
function. If I use a parameter, like file, for this function, I can't not access to the result anymore. For example I'd like to use file.name
in the onload function. How to resolve this issue ?
Event handling is asynchronous and thus they pick up the latest value of all the enclosed local variables(i.e. closure). To bind a particular local variable to the event, you need to follow the code suggested by users above or you can look at this working example:-
http://jsfiddle.net/sahilbatla/hjk3u2ee/
For Typescript;
Try wrapping your onload function in another function. Here the closure gives you access to each file being processed in turn via the variable
f
:For a discussion of why a closure is required here see these related questions:
As the variable file is within the scope, you may use the file variable without passing it to function.
You should use closure at 'onload' handler. Example: http://jsfiddle.net/2bjt7Lon/