有没有办法在http.request of angular 2+中同时发送FormData和json对象?我需要angular2 +的解决方案,而不是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);
答案 0 :(得分:1)
您必须像下面的示例一样在“ formData”对象上传递文件和JSON对象:
public submitForm(picture: File, data: any): Observable<any> {
const formData = new FormData();
formData.append('picture', picture);
formData.append('data', JSON.stringify(data));
}
答案 1 :(得分:-1)
您应该将文件附加到正文。
let options = new RequestOptions({ headers: headers });
options.method = 'POST';
options.body = data;
options.body.append('file', formData);
然后提出请求
this.http.request(url, options);