I have the code below where I upload a file and I want to get its contents in a variable. For this I use the onload function that gives me the result, which is the contents of the file. But the variable gives undefined out of onload, how to make that result be able to be used outside of this function?
if (event.target.files.length> 0) {
let fileContent;
let file = event.target.files [0];
let fileReader = new FileReader ();
fileReader.onload = function () {
fileContent = fileReader.result;
console.log (fileContent); //file content result
};
fileReader.readAsText (file);
console.log (fileContent); //undefined
}
}
PS: I need to know how to do it in Angular, not jQuery.
Thanks