Php 未使用codeigniter中的jquery上载图像

Php 未使用codeigniter中的jquery上载图像,php,jquery,codeigniter,Php,Jquery,Codeigniter,我正在尝试使用jquery将图像上传到codeginter中的文件夹。但我无法得到什么是确切的问题,为什么图像没有得到上传和显示给我的消息 您没有选择要上载的文件 我无法得到为什么文件没有被选择在这里上传。她是我的php代码 public function add_new_book() { $image = $this->input->post('bookfile'); $img=$this->input->post('bookfile');

我正在尝试使用jquery将图像上传到codeginter中的文件夹。但我无法得到什么是确切的问题,为什么图像没有得到上传和显示给我的消息

您没有选择要上载的文件

我无法得到为什么文件没有被选择在这里上传。她是我的php代码

public function add_new_book()
    {

    $image = $this->input->post('bookfile');
    $img=$this->input->post('bookfile');
    $config['upload_path'] = '/uploads'; 
    $config['overwrite'] = 'TRUE';
    $config["allowed_types"] = 'jpg|jpeg|png|gif';
    $config["max_size"] = '1400';
    $config["max_width"] = '1400';
    $config["max_height"] = '1400';
    $this->load->library('upload', $config);

    if(!$this->upload->do_upload('bookfile')) 
    {               
        $this->data['error'] = $this->upload->display_errors(); 
        echo json_encode(array("result"=>$this->data['error']));
        exit;
    } 

    }
我在这里编写jquery代码

$( document ).ready(function() { 
     $("#btnsavebook").click(function(){ 

if($("#bookfileid").val() != ''){
        if (typeof FormData !== 'undefined') {

    var formData = new FormData($("#form-upload")[0]);
    console.log("formdata:",formData)
    $.ajax({
      type: "POST",
      url: "CreateBook/add_new_book",
      data: formData,
      mimeType:"multipart/form-data",
      dataType: 'json',
      xhr: function() {
            var myXhr = $.ajaxSettings.xhr();
            return myXhr;
      },
      cache:false,                    
      contentType: false,
      processData: false,
      success: function(result){ 
      }                       
    }); 
   } } 
});});
任何人请告诉我如何实现这个过程

谢谢你试试这个

查看文件

控制器


来源:

是否输入类型文件名为“bookfile”?yess@cssblaster21895查看我的案例解决方案,我会帮你确定,只需从我的案例中上传单个图像即可。$img=$this->input->post'bookfile';此行可能会影响$\u文件,可能会将其删除。还可以使用echo json检查$\u文件中的内容。
    <html>
<head>
<title>Ajax Image Upload Using PHP and jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
<div id="selectImage">
<label>Select Your Image</label><br/>
<input type="file" name="my_image" id="my_image" required />
<input type="submit" value="Upload" class="submit" />
</div>
</form>
<h4 id='loading' >loading..</h4>
<div id="message"></div>
</body>
</html>
<script type="text/javascript">

$(document).ready(function (e) {
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
$("#message").empty();
$('#loading').show();
$.ajax({
url: "<?php echo base_url('test/hello'); ?>", // Url to which the request is send
type: "POST",             // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false,       // The content type used when sending data to the server.
cache: false,             // To unable request pages to be cached
processData:false,        // To send DOMDocument or non processed data file it is set to false
success: function(data)   // A function to be called if request succeeds
{
$('#loading').hide();
$("#message").html(data);
}
});
}));
});
</script>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

    public function __construct ()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('test');
    }

    public function hello()
    { 
        // print_r($_FILES['file']); die;
        $config['upload_path']        = 'uploads';
        $config['allowed_types']        = 'gif|jpg|png|jpeg'; // allowed file formats
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('my_image')) 
        {
            echo $this->upload->display_errors(); // It will return errors if file not uploaded
        }
        else
        {
            echo "uploaded";
        }
    }
}