Here's my file reader:
p.read = function(file) {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
return e.target.result;
}
}
But nothing is returned? Any ideas why?
Here's my file reader:
p.read = function(file) {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
return e.target.result;
}
}
But nothing is returned? Any ideas why?
I'm afraid that you cannot return it from a callback function
try to separate it in another function
I hope I helped
This is an async function. That mean that if you do this :
p.read = function(file) {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
console.log('a');
fileReader.onload = function(e) {
console.log('b');
return e.target.result;
}
}
and :
console.log('hello');
p.read('file');
console.log('c');
You will get
hello
c
a
b
Because javacript is asynchronous. Very nice b/c like so, your code is non-blocking: it is really easy to do something while reading a big file.
In your example, you have to do :
p.read = function(file, callback) {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
callback(e.target.result);
}
}
And to run:
p.read('file', function(content) {
console.log(content);
});