1

I am a new programmer. I found many solution regarding this issue but failed to solve my problem.
I am trying to upload multiple images in web.

The HTML:

<input type="file" multiple="multiple" name="uploadcertbtn[]"  />

I append dynamic number or rows below the above row in this way, so I have multiple input type=file button, The JS for this purpose:

$(document).ready(function () {
  $('#addrow').click(function () {
    $('.tbody').append("  <tr ><td ><input type='file'  multiple='multiple' name='uploadcertbtn[]'  /></td></tr> ");
  });
});

Javascript to add file objects in a variable:

var $certimages = [];

$("input[name='uploadcertbtn[]']").each(function(){
  for(var i=0;i<$(this).get(0).files.length;++i){
    $certimages.push($(this).get(0).files[i]);
  }
});

I Test the $certimages array value in this way and it is printing file objects properly.

for(var i=0; i<$certimages.length; i++){
  console.log($certimages[i]);
}

Then I made a FormData object and append the variable in this way:

var formData = new FormData();
formData.append('certificateImagesArray[]', $certimages );

Then I tried to print the formdata values in this way but $certimages variable prints only:

[object File],[object File]

Code to print the formdata values:

for (var value of formData.values()) {console.log(value);}

Please help me to print the file object properly.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
robin
  • 31
  • 2
  • 6
  • 1
    Try `for (var value of formData.values()) {console.log(JSON.stringify(value));}` to console.log the objects content as strings... If that is the question (since I'm not sure). Read more about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) – Louys Patrice Bessette Sep 10 '17 at 08:16
  • i tried but it prints the same as before. – robin Sep 10 '17 at 08:20
  • 1
    @robin stop being afraid of json and grasp the thiny [beast](http://json.org/). JSON was made simple and the spec can fit on a contact card. – aloisdg Sep 10 '17 at 08:22
  • The thing is that the `objects` you handle ARE using the JSON structure... That's why stringify works on it. There is no fear to have in learning some more. ;) – Louys Patrice Bessette Sep 10 '17 at 08:26
  • i tried JSON.stringify() but it prints the same string as before but i want to print the File object which i want to upload to database via java code later. – robin Sep 10 '17 at 08:29
  • If it prints the same... I guess the script is in an external `.js` file and you did not clear the cache. – Louys Patrice Bessette Sep 10 '17 at 08:29
  • @ Louys Patrice Bessette i used internal .js script:) – robin Sep 10 '17 at 08:30
  • After a bit reading on it, I found [this answer](https://stackoverflow.com/a/42126092/2159528), which could interest you. – Louys Patrice Bessette Sep 10 '17 at 09:01
  • I am grateful to you @Louis. but which i need is to get a File{......}object in the console. – robin Sep 10 '17 at 09:15

0 Answers0