After upgrading to Angular 11, I encountered the following error with my code snippet:
const file = (event.target as HTMLInputElement).files[0];
which says Object possibly null
Here is my code in the OnInit class:
imagePreview : string | any;
form: FormGroup | any;
This is the imagepicker function I am using:
onImagePicked(event: Event) {
const file = (event.target as HTMLInputElement).files[0];
this.form.patchValue({
image: file
});
this.form.get('image').updateValueAndValidity();
const reader = new FileReader();
reader.onload = () => {
this.imagePreview = reader.result as string;
}
reader.readAsDataURL(file);
}