2015-04-17 41 views
0

我可以請求你的幫助!如何使用jquery向codeigniter控制器提交文件

我想上傳一個文件,並使用jquery將所有數據傳遞給codeigniter控制器。

我的視圖文件:

<div class="form-group"> 
    <label class="col-md-4 control-label">Item Image</label> 
    <div class="col-md-8"> 
     <input class="form-control" type="file" placeholder="" id="itemImage" name="itemImage" data-parsley-required="true" /> 
    </div> 
</div> 
<button type="button" onclick="add_item();">Sumit</button> 
<script> 
function add_item(){ 
    $.post('<?php echo site_url('admin_newitem/add_item'); ?>', 
     { 
      itemId   : $('#itemId').val(), 
      itemImage  : $('#itemImage').val() 
     }, 
     function(data){ 
       if(data.result=='SUCCESS'){ 
       $('#itemId').val(''); 
       $('#itemImage').val(''); 
        //show success 

        }else{ 
        //show error 
        } 
      },'json' 
     ); 
    } 
</script> 

我的控制器:

function add_item(){ 
    $this->output->set_status_header(200); 
    $this->output->set_header('Content-Type: application/json'); 
    $itemId   = $this->input->post('itemId'); 
    $itemImage  = $this->input->post('itemImage');; 
    $config['upload_path'] = realpath(APPPATH.'../assets_main/img/item/'); 
    $config['allowed_types'] = 'jpg|jpeg'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '200'; 
    $config['max_height'] = '200'; 
    $file_name = $config['file_name'] = md5(uniqid(mt_rand())); 
    $config['remove_spaces'] = TRUE; 
    $this->load->library('upload', $config); 
    if(!$itemImage){ 
     $jdata['result'] = "FAILED"; 
     $jdata['message'] = "upload error."; 
     $jdata['field'] = "itemImage"; 
     echo json_encode($jdata); 
     exit(); 
    } 
    $data = array('upload_data' => $this->upload->data()); 
    $jdata['message'] = "Thank you for purchasing our item!"; 
    $jdata['result'] = "SUCCESS";     
    echo json_encode($jdata); 
    exit(); 
} 

我能拿到項目Id和ItemImage但是當我檢查該圖像是假設所在的文件夾,它不在那裏。我想我無法捕捉到真實的文件,那麼,我有辦法完成這個任務嗎?

回答

0

按照你的建議,我相信你忘記真正做到上傳,代碼點火器文檔可能幫助:

http://www.codeigniter.com/user_guide/libraries/file_uploading.html

有了這個正確實施,我覺得你的代碼應類似於東西:

function add_item(){ 
    $this->output->set_status_header(200); 
    $this->output->set_header('Content-Type: application/json'); 
    $itemId   = $this->input->post('itemId'); 
    $itemImage  = $this->input->post('itemImage');; 
    $config['upload_path'] = realpath(APPPATH.'../assets_main/img/item/'); 
    $config['allowed_types'] = 'jpg|jpeg'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '200'; 
    $config['max_height'] = '200'; 
    $file_name = $config['file_name'] = md5(uniqid(mt_rand())); 
    $config['remove_spaces'] = TRUE; 
    $this->load->library('upload', $config); 


    //THE MISSING LINE 
    if($this->upload->do_upload()){ 
     //THE FILE HAS NOW BEEN UPLOADED 
     $data = array('upload_data' => $this->upload->data()); 
     $jdata['message'] = "Thank you for purchasing our item!"; 
     $jdata['result'] = "SUCCESS";     
     echo json_encode($jdata); 
     exit(); 
    }else{ 
     //NO FILE UPLOADED 
     $jdata['result'] = "FAILED"; 
     $jdata['message'] = "upload error."; 
     $jdata['field'] = "itemImage"; 
     echo json_encode($jdata); 
     exit(); 
    } 


} 
+0

我試過了,但沒有奏效。我想也許是因爲控制器無法捕捉要上傳的文件。 – Dennis

0

我的初步想法,已經使用CI和試圖做同樣的事情,是你不能沒有特殊的插件,通過AJAX做文件上傳這樣的代碼:

http://malsup.com/jquery/form/

具體來說,此文件上傳部分:

http://malsup.com/jquery/form/#file-upload

只是試圖發送一個文件輸入將無法正常工作的VAL後,我不相信 - 但也許更現代瀏覽器現在能夠處理這些問題。

+0

你有什麼其他建議我可以用這個做什麼?如果這真的不會奏效,最有可能的是我會再次正常上傳代碼。謝謝你的答案。我很感激。 – Dennis

+0

你只需要按照插件顯示的那樣設置它,然後創建一個php頁面來抓取上傳文件,類似於你所擁有的。對不起,這就是我所知道的一切。 – dgig

+0

無論如何,我只是決定,如果用戶將上傳圖片,然後他/她需要點擊提交與img或提交沒有img。 – Dennis

相關問題