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
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?enctype : 'multipart/form-data'makes no sense. $.ajax doesn't have an enctype parameterI 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[]'...