2

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]

Darshan Patel
  • 2,639
  • 1
  • 21
  • 36
  • File which is an image ,Am trying to append array1 into counselor,but in return am getting [Object Object] – sam_programmer Nov 13 '17 at 08: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 Nov 13 '17 at 08: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 Nov 13 '17 at 08: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 Nov 13 '17 at 08:58

1 Answers1

4

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]); 
}
G07cha
  • 3,546
  • 1
  • 20
  • 38