如何在formData中附加数组,其中包含2个带键的值

时间:2017-11-13 08:14:27

标签: javascript angularjs multipartform-data

代码:

let options = new RequestOptions({ headers: headers });
let array1;

array1 = [{ "subfolder_name": subfolder, "file_upload": file }];
let formData: FormData = new FormData();
formData.append("folder_name",folder );
formData.append("counselor",array1 );
  

它返回顾问:[对象]

1 个答案:

答案 0 :(得分:1)

FormData是构造键值对集合的接口,因此,它不支持任何类型的嵌套结构,但是,您可以表示嵌套在键中,如下所示:

let array1 = [{ "subfolder_name": "foo", "file_upload": "bar" }];
let formData = new FormData();
formData.append('counselor[0].subfolder_name', array1[0].subfolder_name );
formData.append('counselor[0].file_upload', array1[0].file_upload );

for (let pair of formData.entries()) {
    console.log(pair[0] + ': ' + pair[1]); 
}