I was explained (here btw) that using ViewChild I could access to a NgForm, its data, and be able to modify it before submit. So I have the following code:
@ViewChild('frmRegister',{static:true}) registerform: NgForm;
onRegister(form):void{
this.authService.register(form.value).subscribe(res =>{
this.router.navigateByUrl('/');
})
}
async file2Base64(archivo, origen) {
const file = archivo.target.files[0];
this.file2Base64result = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
if (origen == "avatar") this.registerform.value.avatar = this.file2Base64result;
if (origen == "signature") this.registerform.value.signature = this.file2Base64result;
}
and html goes like:
<form #frmRegister="ngForm" class="login-container" (ngSubmit)="onRegister(registerform)">
//part of form
<p><label name="signature">Firma Personal [PNG]</label>
<input type="file" name="signature" placeholder="Firma Personal" ngModel (change)="file2Base64($event, 'signature')"></p>
<p><label>Avatar</label>
<input type="file" name="avatar" placeholder="Avatar" ngControl="avatar" ngModel (change)="file2Base64($event, 'avatar')"></p>
<p><input type="submit" [disabled]="frmRegister.invalid" value="Register"></p>
</form>
As you see, when the input file changes, file2Base64 is called. It should get the file and transform it into a base 64 string. However, When I submit it, the default value "C:/fakepath/image.png" is sent to the server. No matter what I do the default value overwrites the value I'm trying to overwrite, which is the base64 string.