如何使用jQuery方法$ .post和Codeigniter上传文件?

时间:2018-12-10 03:09:13

标签: jquery codeigniter

我有一个表单,我想提交没有刷新页面的数据。我有一个input type file,我无法上传文件,数据为空。这是我的input type file

的代码
<input type="file" class="form-control custom-file-input" name="uploadFile" id="uploadFile">

这是我的jQuery代码,用于不刷新页面即可提交数据:

var judul = $("input[name=judul]").val();
var lokasi = $("input[name=lokasi]").val();
var satuan_kerja = $("select[name=satuan_kerja]").val();
var mulai = $("input[name=mulai]").val();
var akhir = $("input[name=berakhir]").val();
var ruang_lingkup = $("select[name=scope]").val();
var kategori = $("select[name=kategori]").val();
var keterangan = $("textarea[name=keterangan]").val();
var lampiran = $("input[name=uploadFile]").val(); //this is my input file
var agenda_pimpinan = $("input[name=agenda_pimpinan]").val();

$.post("kalender/add_event", {
     judul: judul,
     lokasi: lokasi,
     satuan_kerja: satuan_kerja,
     mulai: mulai,
     akhir: akhir,
     ruang_lingkup: ruang_lingkup,
     kategori: kategori,
     keterangan: keterangan,
     lampiran: lampiran,
     agenda_pimpinan: agenda_pimpinan
}

如果我打印变量 lampiran ,则值为C:\fakepath\image.jpg。这是我的控制器,用于将所有数据发送到数据库:

public function add_event() {
        // Our calendar data
        $judul = $this->input->post('judul');
        $lokasi = $this->input->post('lokasi');
        $satuan_kerja = $this->input->post('satuan_kerja');
        $waktu_mulai = $this->input->post('mulai');
        $waktu_berakhir = $this->input->post('akhir');
        $scope = $this->input->post('ruang_lingkup');
        $kategori = $this->input->post('kategori');
        $keterangan = $this->input->post('keterangan');
        $agenda_pimpinan = $this->input->post('agenda_pimpinan');

        if($agenda_pimpinan == "on") {
            $agenda_pimpinan = 0;
        }
        else {
            $agenda_pimpinan = 1;
        }

        // var_dump($agenda_pimpinan); die;

        //upload file
        $config['upload_path']          = 'uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;

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

        $this->upload->do_upload('lampiran');
        $file = $this->upload->data();
        $fileInfo = $file['file_name'];
        // end upload file

        $data = array(
                'judul' => $judul,
                'mulai' => $waktu_mulai,
                'berakhir' => $waktu_berakhir,
                'lokasi' => $lokasi,
                'scope' => $scope,
                'satuan_kerja' => $satuan_kerja,
                'keterangan' => $keterangan,
                'kategori' => $kategori,
                'lampiran' => $fileInfo,
                'tampilkan_agenda_pimpinan' => $agenda_pimpinan
        );
        // var_dump($data); die;
        $datas = $this->KalenderModel->addEvent($data);

    } //end function

如果我提交数据,数据库中的兰皮兰列仍为空,则控制器无法获取上载文件的数据。如何解决?

0 个答案:

没有答案