Is there a way to send FormData and a json object at the same time in http.request of angular 2+? I need the solution for angular2+, not to angularjs.
let data = {id: 1, name: 'test'};
let formData = new FormData();
formData.append('fileData', file); //file from inputfile
let headers = new Headers();
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
options.method = 'POST';
options.body = data; //data is my object
//options.formData= formData; //formData is my FormData with file data to upload
this.http.request(url, options);
You have to pass the file and the JSON object on the "formData" object like this example:
public submitForm(picture: File, data: any): Observable<any> {
const formData = new FormData();
formData.append('picture', picture);
formData.append('data', JSON.stringify(data));
}
You should append the file to the body.
let options = new RequestOptions({ headers: headers });
options.method = 'POST';
options.body = data;
options.body.append('file', formData);
And then make the request
this.http.request(url, options);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.