dropzonejs无法自动提交

时间:2016-01-28 07:32:20

标签: php codeigniter

环境:PHP + dropzone.js + CI(笨)

服务器无法执行上传。但浏览器控制台可以输出信息。

<div class="panel-body">  
<div class="form-group">
    <label class="control-label">File Upload</label>
    <div class="controls">
            <form action="Upload/do_upfile" class="dropzone" id="myDropzone">
                <div class="fallback">
                <input name="userfile" type="file" multiple="" />
                </div>
            </form>
    </div>
</div>          

脚本:

Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("#myDropzone", {
    url: "Upload/do_upfile",
    method: 'post',
    addRemoveLinks: true,
    uploadMultiple: true,
    init: function() {
        this.on("addedfile", function(file) {
            console.log("File " + file.name + "add");
        });
        this.on("success", function(file) {
            console.log("File " + file.name + "uploaded");
        });
        this.on("removedfile", function(file) {
            console.log("File " + file.name + "removed");
        });
    }

});

php ci controllers

public function do_upfile()
{
    $config['upload_path']      = './uploads/';
    $config['allowed_types']    = 'gif|jpg|png|css';
    //$config['max_size']     = 100000;
    //$config['max_width']        = 1024;
    //$config['max_height']       = 768;

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

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }


}

浏览器,上传是正常的,控制台输入也有内容,但不在服务器上执行,这些!

1 个答案:

答案 0 :(得分:0)

你必须返回(json_encoded)字符串。 在您的控制器中:

if ( ! $this->upload->do_upload('userfile'))
{
    $error = array('error' => $this->upload->display_errors());

    echo json_encoded($error);
}
else
{
    $data = array('upload_data' => $this->upload->data());

    echo json_encoded($data);
}