1

I'd like to do multiple upload using formData() and AJAX. My AJAX successfully sent data to controller. This is my script:

$(".upl_user_document").each(function(i){
    regist_data.append('bill_doc-'+i, $(this)[0].files[0]);
});
                    
regist_data.append("data_ci",JSON.stringify(data_form));
regist_data.append("data_epicor",JSON.stringify(JSON_regist));
regist_data.append("<?php echo $this->security->get_csrf_token_name(); ?>",csrfHash);
                
    $.ajax({
        url         : url,                      
        type        : 'POST',                       
        enctype     : 'multipart/form-data',                        
        dataType    : 'JSON',
        contentType : false,
        processData : false,
        cache       : false,
        data        : regist_data
    })
    .done(function(data){
        
        //SUCCESS                           
    })
    .fail(function(jqXHR, textStatus, errorThrown){
         
        //FAIL              
    });

This script success sent data to the controller. But, I got undefined index bill_doc error in my controller. This is my controller:

//UPLOAD DOC                    
$my_upload  = $_FILES['bill_doc'];
$j_upl      = count($my_upload["name"]);    
$files      = $_FILES;

for($i=0;$i<$j_upl;$i++){                       

    if(isset($_FILES) & !empty($_FILES)){
        $name     = $files['file']['name'];                      
        $tmp_name = $files['file']['tmp_name']; 
    }

    if(!file_exists(FCPATH."asset/asset_form_signup/uploads/".$epicor_id)) {

        mkdir(FCPATH."asset/asset_form_signup/uploads/".$epicor_id, 0755, true);
    }

    $config['upload_path']   = FCPATH."asset/asset_form_signup/uploads/".$epicor_id."/";

    $this->load->library('upload', $config);

    $_FILES['bill_doc']['name']     = $files['bill_doc']['name'][$i];
    $_FILES['bill_doc']['type']     = $files['bill_doc']['type'][$i];
    $_FILES['bill_doc']['tmp_name'] = $files['bill_doc']['tmp_name'][$i];
    $_FILES['bill_doc']['error']    = $files['bill_doc']['error'][$i];
    $_FILES['bill_doc']['size']     = $files['bill_doc']['size'][$i];

    $this->upload->initialize($config);

    $this->upload->do_upload('bill_doc');

    }

    $response_array = array ( "insert_message"  => "SUCCESS",
                              "token"           => $new_token);

    echo json_encode($response_array);
}

Do I do some mistake? The AJAX show no error. Any helpis greatly appreciated

9
  • 1) regist_data.append('bill_doc', upl_bill_id); regist_data.append('bill_doc', upl_bill_tax); sends two values with the same parameter name. One of them will get overwritten. Is that a typo? Commented Feb 17, 2021 at 9:45
  • 2) enctype : 'multipart/form-data' makes no sense. $.ajax doesn't have an enctype parameter Commented Feb 17, 2021 at 9:46
  • @ADyson 1.) I am about todo multiple upload and want to loop the uploading proceess in the controller Commented Feb 17, 2021 at 9:55
  • Other than that, based on an example such as stackoverflow.com/a/31110402/5947043 there seems to be nothing obviously wrong with your code. When you say "data is successfully sent", do you mean the request doesn't fail entirely? But have you checked whether the request actually contains the data you expect? As in, did you look in the network tab to see it? Commented Feb 17, 2021 at 9:55
  • I am about todo multiple upload and want to loop the uploading proceess in the controller...sure, but that wasn't my point. You're overwriting one value with another. They either must have different names, or you need to make it an array e.g. regist_data.append('bill_doc[]'... Commented Feb 17, 2021 at 9:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.