Codeigniter:错误您未选择要上传的文件

时间:2018-09-26 14:53:51

标签: jquery ajax codeigniter controller image-upload

我正在尝试在我们的webplattform上实现图像上传功能。 目前,我收到错误消息“您未选择要上传的文件”。 它必须在没有提交的情况下工作,因为我不想刷新页面,并且提交按钮将触发其他基本平台功能。

我的表格:

<div id="selectMotiv">
  <?php echo form_open_multipart('upload/image');?>
   <input type="file" accept="image/*" id="motivImage" name="motivImage"/>              
   <button class="btn btn-outline-primary" id="buMotivUpload" type="submit">Upload</button>
  <?php echo form_close(); ?>
  <div id="uploaded_image">  
  </div> 
 </div>

我的js:

$("#buMotivUpload").click(function (e) {
//$('form').on('submit', function (e)) {
  //e.preventDefault();
  console.log($('#motivImage').val());
  var upload_image = $('#motivImage').val();
  if ($('#motivImage').val() == ''){
    alert("Please Select the File");
  } else {
    console.log(upload_image);
    $.ajax({
      url: 'apps/upload',
      method: "POST",
      data: upload_image,
      contentType: false,
      datatype: "json",
      cache: false,
      processData: false,
      success: function (data){
        $('#uploaded_image').html(data);
      }
    });
  }
});

我的控制器:

<?php

  defined('BASEPATH') OR exit('No direct script access allowed');

  class Upload extends CI_Controller {

  public function index() {  

    $data = $_POST;

    if(isset($_FILES["motivImage"]["name"])){  
      $config = array(
        'upload_path' => "./uploads/",
        'allowed_types' => "gif|jpg|png|jpeg",
        'overwrite' => TRUE,
        'max_size' => "2048000" // Can be set to particular file size , here it is 2 MB(2048 Kb)
        //'max_height' => "768",
        // 'max_width' => "1024"
      );
      $this->load->library('upload', $config);      

      if($this->upload->do_upload('motivImage')){
        $data = $this->upload->data();
        echo 'success';
        //$this->load->view('upload_success',$data);
      }else{        
        echo $this->upload->display_errors(); 
        //$this->load->view('custom_view', $error);
      }   
    }else{
      echo json_encode($data);
    }
  }
}

0 个答案:

没有答案