CodeIgniter 上传进度

tro*_*ytc 5 codeigniter file-upload progress-bar

我正在尝试使用 CI 2.1.3 使用进度条上传文件。我已经让文件上传工作正常,但是获得这个文件进度并不容易。我已经查看了大量具有不同解决方案的指南,但它们似乎都不起作用,因为大多数都已经过时了(2008 年左右)。

这是我要找的:

  • PHP/jQuery/AJAX/Javascript 解决方案(请不要使用 Flash!)
  • 跨浏览器(IE 不是问题,所以如果它在 IE8 或更低版本中不起作用,那很好)
  • CodeIgniter 上传库兼容

就是这样。我的代码供参考:

PHP:

public function do_upload()
{
    // Configure and load the uploads library
    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'mp4|mov';
    $config['encrypt_name']  = TRUE;
    $config['max_size']      = '2621440'; // 2.5 GB, in kilobytes

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

    if (!$this->upload->do_upload('userfile'))
    {
        $this->session->set_flashdata('error', $this->upload->display_errors('', ''));

        redirect('upload');
    }
    else
    {
        $this->session->set_flashdata('upload_data', $this->upload->data());

        $upload_data = $this->upload->data();
        $uploader = $this->flexi_auth->get_user_by_id()->row_array()['uacc_username'];

        $this->load->model('upload_model');
        $this->session->set_flashdata('uaid', $this->upload_model->generate_uaid($upload_data['raw_name']));
        $this->upload_model->create_upload($upload_data['file_name'], $upload_data['raw_name'], $upload_data['client_name'], $upload_data['file_size'], $upload_data['file_path'], $uploader);

        redirect('upload');
    }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="progress progress-striped active" id="upload_progress" style="display: none;">
                    <div class="bar" id="upload_progress_bar" style="width: 0%;"></div>
                </div>
                <?php echo form_open_multipart('upload/do_upload', array('id' => 'upload_form')); ?>
                <input type="file" name="userfile" size="20" />
                <button type="submit" name="upload">Upload</button>
                <?php echo form_close(); ?>
Run Code Online (Sandbox Code Playgroud)

tro*_*ytc 3

实际上,经过一些进一步的研究, Dropzone.JS似乎与 CI 配合得非常好,只需很少的配置和调整。我还没有机会使用它的所有功能,但经过大约 5 分钟的工作后,它已经有了上传进度并且拖放上传工作得很好。