使用ajax文件输入codeigniter

时间:2016-09-03 09:44:29

标签: php ajax codeigniter

我想使用ajax上传文件,基于codeigniter和file input kartik的协作。

所以我创建了这样的:

<label for="file">Upload CSV 's</label>
<input name="egt" id="egt" class="file-loading" type="file" multiple data-no="1">

JS

$("#egt").fileinput({
        dropZoneEnabled: false,
        uploadUrl: "<?php echo site_url('admin/kecil/c_daily_egt/create_daily/egt') ?>",
        uploadAsync: true,
        maxFileCount: 20,
    });

public function create_daily() {
    $pathToUpload = "./assets/uploads/";
    $dir_exist = true; // flag for checking the directory exist or not
    if (!is_dir($pathToUpload)) {
        mkdir($pathToUpload, 0777, true);
        $dir_exist = false; // dir not exist
    }


    $name_file = $_FILES['egt']['name']; //initialize name of file

    $config['upload_path'] = $pathToUpload;
    $config['file_name'] = $name_file;
    $config['allowed_types'] = 'csv';
    $config['file_name'] = '';
    $config['max_size'] = '50000';
    $config['overwrite'] = TRUE;

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

    if (!empty($_FILES)) {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($name_file)) {
            echo json_encode($this->upload->display_errors());
        } else {
            $upload_data = $this->upload->data();
            echo json_encode("Success" . $upload_data);
        }
    }
}

我刚收到这样的错误:

You did not select a file to upload.

所以,我这样调试:

Array
(
[egt] => Array
    (
        [name] => EGT_STRG.CSV
        [type] => application/vnd.ms-excel
        [tmp_name] => E:\wamp64\tmp\phpE56C.tmp
        [error] => 0
        [size] => 4748
    )
)

任何帮助它如此欣赏。

1 个答案:

答案 0 :(得分:0)

您正在通过文件名引用该文件:

if (!$this->upload->do_upload($name_file)

但是你需要引用你正在上传的文件'egt'。

试试这个:

if (!$this->upload->do_upload('egt')