I have created a function to convert a file to base64 for displaying the file.
ConvertFileToAddress(event): string {
let localAddress: any;
const reader = new FileReader();
reader.readAsDataURL(event.target['files'][0]);
reader.onload = (e) => {
localAddress = e.target['result'];
};
return localAddress;
}
I am trying to use this function in other components like this:
this.coverSrc=this.localization.ConvertFileToAddress(event);
However, when I log this.coverSrc
to the console, it shows me undefined
.
When I log within this bracket:
reader.onload = (e) => {
localAddress = e.target['result'];
};
It displays the value of base64
. But when I log localAddress
outside of the bracket, it shows me undefined
.
How can I properly return the value from the function and use it in other components?