Answer the question
In order to leave comments, you need to log in
Why doesn't uploading files to the server work?
I crop a photo using ngx-image-cropper and try to send the crop to the server (PHP Yii2).
I found several lessons on the Internet, everywhere ± the same algorithm, but I see I'm missing something. So the component:
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
}
uploadAttachmentToServer() {
const fileToUpload: File = new File([this.croppedImage], 'avatar.png');
this.profileService.uploadAvatar(fileToUpload).subscribe(data => {
console.log(data);
}, error => {
console.log(error);
});
}
export const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'multipart/form-data',
}),
observe: 'response' as 'body',
};
uploadAvatar(file: File): Observable<HttpResponse<any>> {
const url = `${this.url}/avatar`;
const formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
return this.http.post<HttpResponse<any>>(url, file, httpOptions);
}
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAKACAYAAAAMzckj...
Answer the question
In order to leave comments, you need to log in
I figured it out, there are two errors:
1) I send not a form to the server, but a file, right, this is a typo:
return this.http.post<HttpResponse<any>>(url, formData, httpOptions);
// вместо
return this.http.post<HttpResponse<any>>(url, file, httpOptions);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question