javascript - Get and read file via input HTML through AJAX and PHP -


i'm trying receive input file html input through ajax php... here html:

<form id="file-form-real" method="post" enctype="multipart/form-data"><input  type="file" class="inputfile" id="carga-excelreal" name="carga-excelreal" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"><label for="carga-excelreal"><img src="img/msexcel_2013_logo.svg"><span>carga archivo costo real</span></label><button type="submit" class="u-button" id="ubutton-real" onclick="uploadmanager.sendreal();">actualizar bd</button></form> 

here javascript file uploadmanager.js:

var uploadmanager = {     sendreal: function(){         var form = document.getelementbyid("carga-excelreal");         var nameor = form.value;         var splited = nameor.split("\\");         var largo = splited.length;         var name = splited[largo-1];         var file = form.files;         var formdata = new formdata();         formdata.append('file', file);         formdata.append('name', name);          var ajaxrequest= $.ajax({               url:"controllers/admin/uploadcostoreal.php",               data: formdata,                type:"post",               contenttype:false,               processdata:false,               cache:false,               success:function(response){                   console.log(response);               }         });     } } 

here php:

<?php     $name = $_post['name'];     $file = $_files['file'];      if(move_uploaded_file($file,"../../docs/costo_real/".$name)){         $msg = "uploaded ok";         echo json_encode($msg, json_unescaped_unicode);     }else{         $msg = "there error";         echo json_encode($msg, json_unescaped_unicode);     } ?> 

when print print_r($_post) , print_r($_files) here output:

array ( ) array (     [file] => [object filelist]     [name] => 1557 - planilla gestion de compras 16.ago.2016.xls ) 

i have following questions:

-how can read [object filelist], file , save specific folder.

-(i've saw [file] empty, .... why??, suposed have file).

-the general procedure ok or have action attribute in form tag???

also might helpful if knows how put loading gif (this last thing not important main thing i've asked)

thank answers.

in js var file = form.files; needs be:

var file = form.files[0]; or var file = form.files.shift();

i believe form.files array.


Comments