Upload image with HttpClient

后端 未结 2 669
庸人自扰
庸人自扰 2020-12-08 15:46

Problem

I try to upload an image with angular\'s HttpClient to API Content-Type: multipart/form-data (angular v4+). Is it supported? How to do it?

2条回答
  •  囚心锁ツ
    2020-12-08 16:11

    I don't think the issue is with angular, I used the same code to send a file to the server (I am using spring in backend), the only difference is I am not observing the request or converting the response to promise.

    See my code below:

    let formData: FormData = new FormData();
    formData.append('file', this.fileToUpload);
    
    this.http.put(this.usersUrl, formData).subscribe(
        data => {
            console.log(data); 
        },
        error => {
            console.log(error);
        }
    );
    

提交回复
热议问题