您没有选择要上传的文件(codeigniter)

时间:2017-10-08 15:14:37

标签: php html ajax codeigniter

我正在尝试上传已使用以下代码的图片。

HTML :(我希望在不提交表单的情况下上传图片。)

    <form id="chapThumb" method="post" action="<?php echo base_url();?>index.php/Admin/createChapter" enctype="multipart/form-data">
         <input type="file" name="thumbpic" id="thumbpic">
         <p class="FS12">Upload a thumbnail Image for the Chapter</p>
         <input type="text" class="form-control" name="chname" placeholder="Enter chapter name"><br>
         <input type="button" class="form-control" name="thumb" value="Upload" id="thumb">
         <input class="dp-btn2" type="submit" value="Create" name="submit">
    </form>

JQuery的:

$(document).ready(function(){
            $("#thumb").on('click',function(){
                var x = $("#thumbpic").val();
                console.log('x:'+x);
                jQuery.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: "<?php echo base_url();?>index.php/Admin/createChapterThumb",
                    data: {"thumbpic":x},
                    success:function(response){
                        console.log("response"+response);
                        var msg = response.message;
                        var stat = response.status;
                        var da = response.da;
                        console.log('msg:'+msg+',stat:'+stat+',da:'+da);
                        if(stat == 'success'){
                            //some code
                        }
                        else{
                            //some code
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) { 
                        console.log("Status: " + textStatus); console.log("Error: " + errorThrown); 
                    }
                });
            });
        });

控制器:

public function createChapterThumb(){
        if($this->input->is_ajax_request()) {
            $this->load->model('DPModel');
            $config['upload_path']          = 'uploads/chapters/thumbs/temp';
            $config['file_name']            = 'chapThumb';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload('thumbpic'))
            {
                $data['status'] = 'error';
                $data['message'] = $this->upload->display_errors();
                echo json_encode($data);
            }
            else
            {
                $data['status'] = 'success';
                $data['message'] = $this->upload->data();
                echo json_encode($data);
            }
            exit;
        }
        //$res = $this->customerModel->insertChapter();
    }

点击上传按钮后,我在控制台中收到以下错误:

  

msg:您没有选择要上传的文件。,stat:error

1 个答案:

答案 0 :(得分:0)

将控制器功能中的行$config['upload_path'] = 'uploads/chapters/thumbs/temp';更改为下方。

$config['upload_path'] = './uploads/chapters/thumbs/temp/';

此外,将文件夹的文件权限设置为777。