admin管理员组

文章数量:1401158

Code :

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

it return counselor:[Object Object]

Code :

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

it return counselor:[Object Object]

Share Improve this question edited Nov 13, 2017 at 8:47 Darshan Patel 2,8972 gold badges26 silver badges39 bronze badges asked Nov 13, 2017 at 8:14 sam_programmersam_programmer 513 silver badges10 bronze badges 4
  • File which is an image ,Am trying to append array1 into counselor,but in return am getting [Object Object] – sam_programmer Commented Nov 13, 2017 at 8:19
  • Could you provide some more context? It would be useful to see the code that is returning the unwanted counselor:[Object Object] and also it would be useful to see the bigger picture. What are you trying to do in general? – Rocky Sims Commented Nov 13, 2017 at 8:30
  • Am trying to post a parameter 'folder_name' and 'counselor folder',In counselor value am adding an array ,in that array which includes sub_folder as a text value and file_upload as one iimage upload ,these all am trying in angular 4 – sam_programmer Commented Nov 13, 2017 at 8:41
  • The only valid types for FormData's field-value are USVString and Blob/File. To send an object structure, you will have to stringify it (generally with JSON.stringify). File objects can't be stringified, you will have to give them their own field. – Kaiido Commented Nov 13, 2017 at 8:58
Add a ment  | 

1 Answer 1

Reset to default 5

FormData is an interface to construct set of key-value pairs, therefore, it does not support any kind of nested structure, but you could, however, denote nesting in the key like so:

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]); 
}

本文标签: