I have run into a problem where I can't seem to get a value from an asynchronous JavaScript method I am running in Jquery. My Jquery looks like this:
$(document).ready( function() {
$('#splash_image_upload').change( function() {
var file = this.files[0];
var blob_string = create_blob(file);
alert(blob_string);
});
I am able to access the value that comes from the 'onload' event but I cannot seem to return the actual value. I have tried this:`
function create_blob(file) {
var reader = new FileReader();
reader.onload = (function() { return function(e) { return e.target.result; }; })();
reader.readAsDataURL(file);
}
Every time I execute this function the value of the 'blob_str' variable is 'undefined' presumably because the assignment is done before the function is finished. I am not really sure how to go about this. Is there a way I can return this value from this function??
See Question&Answers more detail:os