Ajax upload file created using print codeigniter

View: 481    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Codeigniter   Fields: Other

0 point/2 review File has been tested

In this tutorial I will explain you how to upload file using ajax in codeigniter.

Introduction

n the last tutorial of file upload I have explained you Upload multiple files using codeigniter . In this tutorial I will explain you how to upload file using ajax in codeigniter. I have seen many user get difficulties to upload while using ajax to upload file. To upload file using ajax you just need to add some code inside your Ajax. Now create a file name Test.php inside the view folder and copy and paste the below code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
 
<script type="text/javascript">
function sendData()
{
 
    var data = new FormData($('#posting_comment')[0]);
 
 
     $.ajax({
               type:"POST",
               url:"<?php echo site_url('Test/fileUpload');?>",
               data:data,
               mimeType: "multipart/form-data",
                contentType: false,
                cache: false,
                processData: false,
               success:function(data)
              {
                        console.log(data);
 
               }
       });
 
}
 
</script>
<form name="posting_comment" id="posting_comment" >
 
      <input id="file_upload" name="attachment_file" class="file_upload_icon" type="file"/>
      <input type="button" class="post postbtn" style="border: none;outline:none;" value="Post" onclick = "return sendData()"/>
</form>
 In the above code we will get the all form data in the data variable and we have used onclick function inside button to run the function sendData()     and call the controller in ajax url. Do not forget to add mimeType: “multipart/form-data” is very important for file upload.

Now create the controller name Test.php   inside the controller folder and copy and paste the following code. In the code we will autoload the view file and will make a function fileUpload() that will be used to get the file name and uplaod the file in the folder.

load->library('upload');
       $this->load->helper(array('url'));
       $this->load->view('Test');
 } 
       public function fileUpload()
           {
 
              $attachment_file=$_FILES["attachment_file"];
              $output_dir = "upload/";
              $fileName = $_FILES["attachment_file"]["name"];
 move_uploaded_file($_FILES["attachment_file"]["tmp_name"],$output_dir.$fileName);
 echo "File uploaded successfully";
           }
 
    }

Hope this tutorial how to upload file using ajax in codeigniter will help you.

Ajax upload file created using print codeigniter

Ajax upload file created using print codeigniter Posted on 08-01-2016  In this tutorial I will explain you how to upload file using ajax in codeigniter. 5/10 481

Comment:

To comment you must be logged in members.

Files with category

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd