I am trying to handle the uploading of images in a form, in order to convert them to base64 and store them as strings in an string[]. For this, Im using FileReader and its method readAsDataURL. When I execute the program, it does not store the strings but if I debug the program, it does. I have no idea why this is happening? Does anybody have any clue?
This is my code, Im using react and TypeScript.
<Input
type="file"
accept='image/*'
onChange={(e) => {
var reader = new FileReader();
let files = e.target.files !== null ? e.target.files : [];
for (let image of files){
reader.readAsDataURL(image)
reader.addEventListener("load", function () {
let srcImage = reader.result;
imgs.push(srcImage as string)
})
}
}}
multiple>
</Input>
'imgs' is a global string[] variable in which i am trying to store the base64 representation of the images.