php - Getting multiple files value by javascript or jquery is not working properly -


i trying send images on server using jquery , ajax method. want upload multiple files not able properly.

my html codes,

<input type="file" name="imgs" multiple class="form-control floating-label" placeholder=""> 

my jquery codes,

var imgdata = new formdata(); imgdata.append('uid', user_id); imgdata.append('o_img', $('input[name="imgs"]').get(0).files); $.ajax({             url : cdn_host + "index.php?action=upload",             type: "post",             data : imgdata,             enctype: 'multipart/form-data',             contenttype: false,             processdata: false,             success: function(data, textstatus, jqxhr)             {                  if(jqxhr.status == 200){                       if(data.error == 'true'){                         alert'(true');                      }else                      if(data.error == 'false'){                         alert('false')                      }else{                         alert(json.stringify(data));                      }                  }else{                     alert('some error');                 }              },             error: function (jqxhr, textstatus, errorthrown)             {                  //$('html').html();                 $('html').html(jqxhr.responsetext);                 //$('html').html(json.stringify(jqxhr));              }         }); 

my php codes are,

if($_post["o_img"]){          $_response = $_post['o_img'];                 } header('content-type: application/json');  echo json_encode($_response); 

the response in json getting "[object filelist]". if use $_files['o_img'], getting undefined variable. can tell me how can use jquery or javascript multiple files , send server via ajax post?

thank time in advance. :d

just use following method simplify post method

 var formdata = new formdata($("form#form id")[0]);   // mention here form id     function save_from() {         $.ajax({             type: "post",             url: "/", // url goes here              data: formdata,             success: function(data) {                //your code goes here             },             cache: false,             contenttype: false,             processdata: false         });  } 

sample form

 <form action="" id="form id" method="post" enctype="multipart/form-data">       <input type="file" name="imgs" multiple >          <input type="button" value="submit" onclick="save_from()" >     </form> 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -