I want to upload one file on server with Ajax function and using formdata object . I was show all value of input file when click button upload .
name: "Lich hoc.txt"
size: 1950
type: "text/plain"
But my trouble is i can't read object in my php code , var_dump is nothing . I used framework Codeigniter . Please help me ! Thanks
HTML CODE
<form enctype="multipart/form-data" name="" id="" method="post" action="">
<input id="file" type="file" name="file" />
<button class="button" id="btnDangKi"> Đăng Kí </button>
</form>
JS CODE
$(document).ready(function(){
$('#btnDangKi').click(function(){
UpLoadFile();
});
});
function UpLoadFile(){
var file_upload = document.getElementById("file").files[0];
var fd = new FormData();
fd.append("file",file_upload);
console.log(file_upload);
$.ajax({
url:'<?php echo base_url('QuanLyXe_Ajax/upload'); ?>',
data: fd,
processData: false,
contentType: false,
type: 'POST',
enctype: 'multipart/form-data',
mimeType: 'multipart/form-data',
success: function(data){
alert(data);
}
});
}
PHP CODE
public function UpLoad(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt';
$this->load->library('upload', $config);
if ($this->upload->do_upload("file"))
{
echo "Thanh cong upload ";
}else
{
echo $this->upload->display_errors();
}
echo var_dump($_POST);
}