Unable to print fileContents array values within a function. On upload file, i want to store file content in a js array then use whenever i need with in a function(like if condition below). But after the doc.onload = function() scope, the fileContents value is not available. it prints empty array.
projects:function(component, event, helper){
var fileName = [];
var fileContents =[];
var fileInput = component.find("fileId");
if(fileInput && !Array.isArray(fileInput)){
var f = fileInput.get("v.files");
var file = f[0][0];
fileName.push(file.name);
var fileContent;
var doc= new FileReader();
doc.onload = function() {
var self=this;
fileContent = doc.result;
var base64 = 'base64,';
var dataStart = fileContent.indexOf(base64) + base64.length;
fileContent = fileContent.substring(dataStart);
fileContents.push(fileContent);
console.log('fileContents-----------------'+fileContents); //prints array with values
};
filer.readAsDataURL(file);
if(somecondition){
console.log('fileContents-----------------'+fileContents); //prints empty array
}
}
Any suggestion would be appreciated?.