2015-06-25 37 views
1

我試圖上傳一個文件到服務器l框架CodeIgniter,jQuery ajax,但我提交表單返回空圖像字段,並且圖片不起來。Codeigniter輸入文件返回空表格與jQuery ajax

我很感謝您的幫助。

下面的代碼

控制器:

public function do_upload() 
{ 
    //Check if isset request AJAX 
    if(! $this->input->is_ajax_request()) { 
     show_404(); 
    } 

    //Config upload files 
    $config = array(
        'upload_path' => './assets/files/', 
        'allowed_types' => 'jpg|jpeg|png|gif', 
        'max_size'  => '2048 ', 
        'overwrite'  => TRUE, 
        'remove_spaces' => TRUE 
        ); 

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

    if (! $this->upload->do_upload('imagen')) { 

     $error = array(
         'respuesta' => FALSE, 
         'message' => $this->upload->display_errors() 
        ); 
     echo json_encode($error); 
    } 
    else{ 
     $config = array(
         'respuesta' => TRUE, 
         'message' => 'Completado!' 
         ); 
     echo json_encode($config); 
    } 


} 

查看:

<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 col_right"> 
<div class="ctn"> 
    <h3>Subir contenido al servidor</h3> 

    <form action="" method="POST" id="form_file" name="form_file" enctype="multipart/form-data"> 

     <div class="form-group"> 
      <input type="file" name="imagen" id="imagen"> 
     </div> 

     <button type="button" id="btn_form" class="btn btn-primary">Subir imagen</button> 
    </form> 

</div><!--End ctn--> 

腳本的Jquery的Ajax:

$(function() { 

    $('#btn_form').on('click', function(event) { 

    //Serialize form 
    var form_serialize = $("#form_file").serialize(); 
    alert(form_serialize); 

    $.ajax({ 
     beforeSend: function() { 

     }, 
     cache: false, 
     type: "POST", 
     dataType: "json", 
     url: "/file/do_upload/", 
     data: form_serialize + "&id=" + Math.random(), 
     success: function (response) { 

      if (response.respuesta == true) { 
       alert(response.message); 
      } 

      if (response.respuesta == false) { 
       alert(response.message); 
      } 

     }, 
     error: function() { 
      alert('SYSTEM ERROR, TRY LATER AGAIN'); 
     } 
    }); 
    }); 

});

回答

2

你有按鈕式做這樣

變化submit ,,

變化jQuery函數

$(function() { 
      $('#form_file').on('submit', function (e) { 
       e.preventDefault(); 
       var formData = new FormData(this); 
       $.ajax({ 
//     cache: false, 
        type: "POST", 
        // dataType: "json", 
        processData: false, 
        contentType: false, 
        url: "/file/do_upload/", 
        data: formData, 
        success: function (response) { 
         if (response.respuesta == true) { 
          alert(response.message); 
         } 
         if (response.respuesta == false) { 
          alert(response.message); 
         } 
        }, 
        error: function() { 
         alert('SYSTEM ERROR, TRY LATER AGAIN'); 
        } 
       }); 
      }); 
     }); 
+0

非常感謝您的回答,我很感激。 我應用了更改,我返回一個值[對象FormData],我也看到一些選項dataType被廢止。 我認爲最好是檢查整個項目以查看它的工作情況,如果這是不可能的,我將整個項目中的更改上傳到Dropbox,但想法是真實示例顯示了完整的功能。 鏈接:https://www.dropbox.com/s/lg2zt0i4h7kf5jk/practica.zip?dl=0 鏈接系統上傳帶有ajax的圖像:http:// localhost/upload_ajax –