-1
我想用codeigniter框架上傳帶有ajax函數的圖像。 $ this-> upload-> do_upload('file'),以及如何獲取像codeigniter中的圖像文件的所有細節,我們使用$ _FILE()。使用codeigniter框架上傳帶有ajax函數的圖像
控制器
class Upload extends CI_Controller{
#code
public function index(){
$this->load->view('upload');
}
public function do_upload(){
if(!empty($_FILES)){
$config = array(
'upload_path' => './uploads/',
'allow_types' => 'gif|jpg|jpeg|png|JPG|JPEG|PNG|GIF',
'max_size' => '1024',
'max_width' => '1024',
'max_height' => '1000',
'encrypt_name' => TRUE,
'overwrite' => TRUE
);
$this->load->library('upload', $config);
//$this->upload->initialize($config);
echo "test1 :<br/>";
print_r($this->upload->do_upload('file'));
echo "test2 : <br/>";
if(!$this->upload->do_upload('file')){
echo "unable to upload images";
}else{
$data = $this->upload->data();
$this->thumb($data);
$file = array(
'filename' => $data['file_name']
);
$this->upload_model->imageupload($file);
$data = array('upload_data'=>$this->upload->data());
}
//redirect(site_url('upload'));
}
}
}
查看 圖片上傳
腳本
<script type="text/javascript">
$(document).ready(function(){
$('#userfile').change(function(){
//$('.img_pre').attr('src','<?php echo base_url(); ?>uploads/ProgressBar.gif');
$('#btn_upload').submit();
});
var inputFile = $('input[name=userfile]');
var uploadURL = '<?= site_url('upload/do_upload')?>';
$('#btn_upload').on('click', function(e){
e.preventDefault();
$('.img_pre').attr('src','<?php echo base_url(); ?>uploads/ProgressBar.gif');
var fileToupload = inputFile[0]. files[0];
if (fileToupload != 'undefined') {
//code
var formData = new FormData();
formData.append('file', fileToupload);
$.ajax({
type: 'POST',
url : uploadURL,
data: formData,
//dataType: 'json',
async: false,
processData:false,
contentType:false,
cache: false,
success: function(data){
}
});
}else{
alert("Error :"+data);
}
});
});
</script>
你會想在Javascript中圖像的數據?或在codeignitor'do_upload()'函數中的圖像數據? –