2017-09-01 139 views
1

有沒有辦法在angular2的http.request中同時發送FormData和json對象?我需要angular2 +的解決方案,而不是angularjs。Angular 2+發送Json數據請求+ FormData

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); 
+1

我終於找到了上傳文件和相同的請求中發送一些JSON之有道,在這裏做了一個正確的答案的要求:https://stackoverflow.com /問題/ 39693966 /如何對angular2-後的JSON數據和文件,在同一的請求/ 47408232#47408232 – Maxime

回答

-1

您應該將文件附加到正文。

let options = new RequestOptions({ headers: headers }); 
options.method = 'POST'; 
options.body = data; 
options.body.append('file', formData); 

然後讓

this.http.request(url, options);