Codeigniter: 'You did not select a file to upload', when data sent through Ajax

Viewed 1169

I'm trying to insert form data through Ajax and Codeigniter but am unable to insert the data, getting 'You did not select a file to upload' error. Facing problem with the file uploads. I have no idea of how to pass input file data through jQuery ajax.

Here is my jQuery code:

$('#saveAddress').click(function(event){
        event.preventDefault();
        var add = $('#address').val();
        var city = $('#city').val();
        var state = $('#sate').val();
        var district = $('#district').val();
        var pcode = $('#pincode').val();
        var pType = $('#proofType').val();
        var aProof = new FormData($('#addressProof'));
        if(add != "" && city != "" && state != "" && district != "" && pcode != "" && pType != "" && aProof != ""){
            $.ajax({
                type:"POST",
                url:"<?php echo base_url();?>index.php/MyApp/addressDetails",
                data:{address:add,city:city,state:state,district:district,pincode:pcode,proofType:pType,addressProof:aProof},
                contentType: false,
                processData: false,
                beforeSend: function() {
                    jQuery('#pbo-loader').css('display','block');
                },
                success:function(response){
                    console.log(response);
                },
                error: function(jqXHR) { 
                    console.log(jqXHR); 
                },
                complete:function(){
                    jQuery('#pbo-loader').css('display','none');
                }
           });   
        }
    });

controller code:

public function addressDetails(){
            if($this->session->userdata('currently_logged_in')){
                $pbonum = $this->session->userdata('pbonum');
                if(isset($_POST)){
                    $noti['add'] = $this->input->post('address');
                    $noti['img'] = $this->input->post('addressProof');
                    $thumb = 'addressProof_'.$pbonum;
                    $config['file_name']            = $thumb;
                    $config['upload_path']          = 'uploads/kyc/address/';
                    $config['allowed_types']        = 'gif|jpg|png|pdf|doc';
                    $config['max_size']             = 2000;
                    $this->load->library('upload');
                    $this->upload->initialize($config);
                    if ( ! $this->upload->do_upload('addressProof'))
                    {
                            $noti['error'] = array('error' => $this->upload->display_errors());
                    }
                    else
                    {
                        $data = array('upload_data' => $this->upload->data());
                        $thumb = $data['file_name'];
                        $res = $this->CustomerModel->updateAddressDetails($thumb);
                        if($res == 1){
                            $noti['status'] = "success";
                        }
                        else{
                            $noti['status'] = "failed";
                        }
                    }
                    print_r($noti);
                }
            }
            else{
                $this->load->view('templates/header');  
                $this->load->view('signin');
                $this->load->view('templates/footer');  
            }
        }
1 Answers

in order to achieve what you want you need to use the standard javascript object here

var aProof = new FormData($('#addressProof')[0]);

Furthermore, you don't need to set your data manually, try the code below

$.ajax({
    type:"POST",
    url:"<?php echo base_url();?>index.php/MyApp/addressDetails",
    data:aProof,
    contentType: false,
    processData: false,
    beforeSend: function() {
        jQuery('#pbo-loader').css('display','block');
    },
    success:function(response){
        console.log(response);
    },
    error: function(jqXHR) { 
        console.log(jqXHR); 
    },
    complete:function(){
        jQuery('#pbo-loader').css('display','none');
    }
});   
Related