I'm currently working on an Angular project where my goal is to implement a feature that allows users to upload an image using an angular reactive form and then store that image in the local storage.
Here is a snippet of the code from "edit.component.html" file:
<form [formGroup]="form" (ngSubmit)="save()">
<label class="head1">Photo</label> <br>
<input type="file" formControlName="Photo">
<button [disabled]="form.invalid">Save</button>
</form>
And here is a glimpse of the code inside "edit.component.ts":
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormArray} from '@angular/forms';
@Component({
selector: 'app-edit',
templateUrl: './edit.component.html',
styleUrls: ['./edit.component.css']
})
export class EditComponent {
form=new FormGroup({
Photo:new FormControl(null,Validators.required),
});
save(){
var profile;
if(localStorage.getItem("profile")==null){
profile={};
}
else{
profile=JSON.parse(localStorage.getItem("profile")!);
}
// Need help with saving the image to localstorage
}
}
Upon adding the line
const photo = this.form.get('Photo') as File;
in the save() function of ts file, I encountered the following error:
Conversion of type 'AbstractControl<null, null> | null' to type 'File' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'AbstractControl<null, null>' is missing the following properties from type 'File': lastModified, name, webkitRelativePath, size, and 6 more.ts(2352)
Could someone please offer guidance or suggest modifications to successfully save the image in localstorage and display it on the HTML page?