When attempting to upload an image, I encountered the error message listed in the question title:
This is my template
<input type="file" formControlName="avatar" accept=".jpg, .jpeg .svg"
#fileInput (change)="uploadImage($event)" />
<div class="avatar-preview">
<ng-template #selectedImage>
<div style.backgroundImage="{{'url('+ imageUrl +')'}}"></div>
</ng-template>
</div>
<button (click)="fileInput.click()"></button>
And this is my component ts
@ViewChild('fileInput') el: ElementRef;
imageUrl: string = "";
this.profileForm = new FormGroup({
avatar: new FormControl(null)
});
public uploadImage(event) {
let reader = new FileReader();
let file = event.target.files[0];
if (event.target.files && event.target.files[0]) {
reader.readAsDataURL(file);
reader.onload = () => {
this.imageUrl = reader.result as string;
this.profileForm.patchValue({
avatar: reader.result
});
}
}
this.cd.markForCheck();
}