I'm trying to convert image into Base64 by following a tutorial, when I'm uploading image from angular 6 project. when I press submit button I could able to get the output as value:"base64 code" and also I could able to get the same image by converting the code into image by using a "Convert Your Base64 to Image". But in the code, it shows "Property 'split' does not exist on type 'string | ArrayBuffer'.Property 'split' does not exist on type 'ArrayBuffer'." error by underlining split in red. I tried similar question & answers too. But it was unable to remove the error.
my code is
import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
@Component({
selector: 'base64-upload',
templateUrl: './base64-upload.component.html'
})
export class Base64UploadComponent {
form: FormGroup;
loading: boolean = false;
@ViewChild('fileInput') fileInput: ElementRef;
constructor(private fb: FormBuilder) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
name: ['', Validators.required],
avatar: null
});
}
onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.form.get('avatar').setValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1]
})
};
}
}
onSubmit() {
const formModel = this.form.value;
this.loading = true;
// this.http.post('apiUrl', formModel)
setTimeout(() => {
console.log(formModel);
alert('done!');
this.loading = false;
}, 1000);
}
clearFile() {
this.form.get('avatar').setValue(null);
this.fileInput.nativeElement.value = '';
}
}
My html code is
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Bob" formControlName="name">
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" id="avatar" (change)="onFileChange($event)" #fileInput>
<button type="button" class="btn btn-sm btn-default" (click)="clearFile()">clear file</button>
</div>
<button type="submit" [disabled]="form.invalid || loading"
class="btn btn-success">Submit <i class="fa fa-spinner fa-spin fa-fw" *ngIf="loading"></i>
</button>
</form>
The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string. Here is an example of how the error occurs.
To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };
The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!
You have to cast the values you want to split.
(<string>reader.result).split(',')[1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With