I am facing some issues in the javascript FileReader and its events. I have created an input tag with type file. Here I am picking the image file from the system directory and want to show the picked image preview. If I want to show preview, I need the blob of the file through file reader event. But sometimes event not getting called and not able to show the preview. I mentioned the code below for your reference.
<input type="file" color="light" round #imgUploader (change)="onSelectFile($event)" accept="image/*">
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.onload = (event: any) => {
var blobContent = event.target.result;
}
reader.onloadstart = (event: any) => {
console.log("onloadstart", event.target.result)
}
reader.onloadend = (event: any) => {
console.log("onloadend", event.target.result)
}
reader.onprogress = (event: any) => {
console.log("onprogress", event.target.result)
}
reader.onabort = (event: any) => {
console.log("onabort", event.target.result)
}
reader.onerror = (event: any) => {
console.log("onerror", event.target.result)
}
reader.readAsDataURL(event.target.files[0]);
console.log("File reader result : ", reader.result);
}
}
Please give a stable solution to show the image preview always.