Send file by jquery ajax to a CodeIgniter Controller -


i need "send" file (from form) , other data codeigniter controller, using ajax. this:

 $("#formuploadajax").on("submit", function(e){         e.preventdefault();         var f = $(this);         var formdata = new formdata(document.getelementbyid("formuploadajax"));         formdata.append("dato", "valor");         //formdata.append(f.attr("name"), $(this)[0].files[0]);         $.ajax({             url: "example/examplerecibe.php",             type: "post",             datatype: "html",             data: formdata,             cache: false,             contenttype: false,      processdata: false         })             .done(function(res){                 $("#mensaje").html("respuesta: " + res);             });     });      ... 

the codeigniter controller validatew file , other data, , creates file in respective folder.

i try lot of code stackoverflow -and other sites-, , nothing works me.

this work me after many attempts.

i push code (html, js y controller -php-):

the view , script:

<html> <head> <title>upload form</title> </head> <body>  <form method="post" class="myform" enctype="multipart/form-data">         <!-- add span , pther stuff here-->         <input type="file" id="foto1" name="userfile" />         <input type="button" value="submit" onclick="submitfile();" /> </form> <script type="text/javascript">     var patharray = "<?php print base_url(); ?>"; </script>    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>     <script>      function submitfile(){         var formurl = "url of php";         var formdata = new formdata($('.myform')[0]);          $.ajax({            url:patharray+'creacion/crearsolicitud/do_upload',                 type: 'post',                 data: formdata,                 mimetype: "multipart/form-data",                 contenttype: false,                 cache: false,                 processdata: false,                 success: function(data){  alert("bien");                 },                 error: function(jqxhr){                       //   var result = json.parse(query);  alert("mal");                 }         }); }      </script>  </body> </html> 

and codeigniter:

    public function do_upload()     {         //condicion de que sea una entrada tipo ajax:         if(!$this->input->is_ajax_request())         {             echo json_encode("jaja");         }         else         {     $config['upload_path'] = './uploads/';         $config['allowed_types'] = 'gif|jpg|png';         //$config['max_size']   = '1000000';         //$config['max_width']  = '10240';         //$config['max_height']  = '768000';          $this->load->library('upload', $config);          if ( ! $this->upload->do_upload())         {             $error = array('error' => $this->upload->display_errors());              echo json_encode($error);         }         else         {             $data = array('upload_data' => $this->upload->data()); echo json_encode($data);         } 

with this, can create file without refresh page, send message.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -