2017-12-27 499 views
1

我想用ajax上傳一個文件以及其他參數。但是,這些文件沒有上傳。如何在codeigniter中使用ajax上傳文件

表單代碼

<form id="first_form" method="post" enctype="multipart/form-data"> 
    <input type="file" id="file" name="file" accept="image/*" onchange="loadFile(event)"> 
    <input type="text" data-validation="url" class="form-control" id="first_name" name="first_name" placeholder="First Name" /> 
    <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name" /> 
    <input type="image" src="<?php echo base_url(); ?>assets/images/icon/_Save.png" class="Save-container img-circle" id="submit_first_form"> 
</form> 

腳本代碼

<script type="text/javascript"> 
    $(document).ready(function() { 
    $("#submit_first_form").click(function(event) { 
    event.preventDefault(); 
    var file = $("input#file").val(); 
    var first_name = $("input#first_name").val(); 
    var last_name = $("input#last_name").val(); 

    jQuery.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>" + "student/add_data", 
     dataType: 'json', 
     data: { 
      file: file, 
      first_name: first_name, 
      last_name: last_name 
     }, 
     success: function(res) { 
     if (res) 
      { 
       console.log(res); 
      } 
     } 
    }); }); }); 
</script> 

控制器代碼

public function add_data() 
    { 
     $data = array(
      'file' => $this->input->post('file'), 
      'first_name' => $this->input->post('first_name'), 
      'last_name'=>$this->input->post('last_name') 
     ); 

     $status = ""; 
     $msg = ""; 
     $file_element_name = $data['file']; 

     $config['upload_path'] = 'www.localhost.com/project/assets/supplier'; 
     $config['allowed_types'] = 'gif|jpg|png|doc|txt'; 
     $config['max_size'] = 1024 * 8; 
     $config['encrypt_name'] = TRUE; 

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

     if (!$this->upload->do_upload($file_element_name)) 
      { 
       $status = 'error'; 
       $msg = $this->upload->display_errors('', ''); 
      } 
     else 
      { 
       $data = $this->upload->data(); 
       //$data['file_name'] 
       $status = "success"; 
       $msg = "File successfully uploaded"; 
      } 
     echo json_encode(array('status' => $status, 'msg' => $msg)); 
    } 

錯誤,我在控制檯我得到的是:

{狀態:「錯誤」,味精:「你沒有選擇要上傳的文件。」}

誰能告訴如何上傳文件

+1

嘗試看https://stackoverflow.com/questions/34151367/codeigniter-3-ajax-file-upload-you-did-not-select -a-file-to-upload – Leo

+1

可能存在dupl [您沒有選擇要上傳的文件。 PHP代碼點火器](https://stackoverflow.com/questions/20115515/you-did-not-select-a-file-to-upload-php-code-igniter) – Vickel

+0

@Vickel嘗試瞭解決方案,但仍然錯誤 – sammy

回答

3

嘗試使用此代碼爲腳本:

<script type="text/javascript"> 
    $(document).ready(function() { 
    $("#submit_first_form").click(function(event) { 
    event.preventDefault(); 
    var form_data = new FormData($('#first_form')[0]); 

    jQuery.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>" + "student/add_data", 
     data: form_data, 
     processData: false, 
     contentType: false, 
     success: function(res) { 
     if (res) 
      { 
       console.log(res); 
      } 
     } 
    }); }); }); 
</script> 
+0

我仍然收到相同的消息,你可以請檢查一下控制器的代碼嗎 – sammy

+0

替換$ file_element_name = $ data ['file'];用$ file_element_name = $ _FILES ['file'];希望這將解決這個問題。 –

+0

現在我得到錯誤作爲未定義的索引:文件 – sammy

0
<form id="first_form" method="post" enctype="multipart/form-data"> 

的加密類型時,文件上傳。

+0

嘗試過但仍然期望的結果是沒有到來 – sammy

0

- >給在形式上標籤ID 提交和之前的形式密切 腳本添加一個提交按鈕發送請求 $(「#拯救」)點擊(函數(事件){ event.preventDefault();

// Create an FormData object 
    var data1 = new FormData($('#fileUploadForm')[0]); 
     console.log($('#fileUploadForm')); 
$.ajax({ 
     type: "POST", 
     enctype: 'multipart/form-data', 
     url: "<?php echo base_url(); ?>" + "student/add_data", 
     data: data1, 
     processData: false, 
     contentType: false, 
     cache: false, 

     success: function (data) { 
     data = JSON.parse(data); 
     console.log(data); 
     if(data.id==1) 
     { 
      localStorage.setItem("user_profile_pic", data.user_pic); 

      alert("updated sucessfully") 

     } 



     } 
    });