I am looking to upload a file onto my server. Here is what I have attempted:
<input (change)="uploadImage($event.target)" hidden accept="image/*" #uploadProfileImage type="file">
uploadImage(event) {
const profileImage = event.files.item(0);
this.profileService.postFile(profileImage).subscribe(data => {
console.log(data);
// do something, if upload success
}, error => {
console.log(error);
});
}
postFile(fileToUpload: File): Observable<string> {
const formData: FormData = new FormData();
formData.append('fileKey', fileToUpload, fileToUpload.name);
console.log(fileToUpload);
return this.httpClient.post<string>('/api//profile/profile-picture', formData);
}
However, I encountered the following error:
Unexpected token I in JSON at position 0
https://i.sstatic.net/mE4n4.png
The server responds with "I have received the request!" as shown in the image, but an error still occurs.
What could be causing this issue?