【问题标题】:You did not select a file to upload(codeigniter)您没有选择要上传的文件(codeigniter)
【发布时间】:2018-03-19 20:23:35
【问题描述】:

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

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

【问题讨论】:

  • 您无法以您尝试的方式获得&lt;input type='file' ...&gt; 的值。您将需要使用 javascript FormData 对象。 Stackoverflow 和网络上其他地方的大量示例。

标签: php html ajax codeigniter


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2013-07-26
    • 2016-03-13
    相关资源
    最近更新 更多