如何上传类型文件使用datatables codeigniter数据库

问题描述 投票:0回答:1

我想将文件名称转换为db,然后将文件转到我的存储服务器?

我的看法

   <form method="post" id="add_form" class="form-horizontal form-label-left" action="<?php echo site_url('bunyi_pasal/save') ?>" enctype="multipart/form-data">
    <div class="form-group"> 
        <label for="file" class="col-sm-1 control-label"> File </label>
        <div class="col-sm-1">
            <input type="file" id="file_pasal" name="file_pasal" class="file file-loading"> 
        </div>
    </div>     
    <div class="col-xs-12 col-md-12 col-lg-12 " align="navbar-right">
        <button type="submit" name="submit" class="btn btn-success btn-lg" style="width:50%">Save</button>
    </div>  
</form>

我的DataTables

<script type="text/javascript">
    $(document).ready(function () {
        var save_method;
        var del = $(this).data('delete');
        $(".select2").select2({width: 'resolve', dropdownAutoWidth: true});
        $('#date').daterangepicker({
            singleDatePicker: true,
            showDropdowns: true,
            format: 'YYYY-MM-DD',
            calender_style: "picker_2"
        }, function (start, end, label) {
            //console.log(start.toISOString(), end.toISOString(), label);
        });
        $('#add_form').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                id_pasal: {
                    validators: {
                        notEmpty: {
                            message: ' Data Pasal Not Empty'
                        },
                    }
                },
                point: {
                    validators: {
                        notEmpty: {
                            message: ' Point Not Empty'
                        },
                    }
                },
                isi_bunyi_pasal: {
                    validators: {
                        notEmpty: {
                            message: ' Bunyi Pasal Not Empty'
                        },
                    }
                },
            },
            submitHandler: function (validator, form, submitButton) {
                $.post(form.attr('action'), form.serialize(), function (result) {
                    //  console.log(result);
                    //window.location.href = result.redirect;
                    if (result.retCode == 0) {
                        new PNotify({
                            title: 'Success',
                            text: result.info,
                            type: 'success',
                            styling: 'bootstrap3'
                        });
                        setTimeout(function () {
                            location.reload(true);
                        }, 1000);
                    } else {
                        new PNotify({
                            title: 'Response',
                            text: result.info,
                            type: 'error',
                            styling: 'bootstrap3'
                        });
                    }
                }, 'json');
            }
        });
    });
</script> 

我的控制器

public function __construct() {
    parent::__construct();
    $this->load->model('General');
    $this->load->helper(array('form', 'url'));
}

public function save() {
    $this->output->enable_profiler(TRUE);
    $response = array();
    if ($this->input->is_ajax_request()) {
        $id_bunyi_pasal = $this->input->post('id_bunyi_pasal');
        if (empty($id_bunyi_pasal)) {
            if (!empty($_FILES['file_pasal']['name'])) {
                //$config['upload_path'] = 'asset/data_pasal/';
                $config['upload_path'] = UPLOAD_DIR . '/asset/data_pasal';
                $config['allowed_types'] = 'doc|docx|pdf|jpg|jpeg';
                $config['file_name'] = $_FILES['file_pasal']['name'];
                //Load upload library and initialize configuration
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if ($this->upload->do_upload('file_pasal')) {

                    $uploadData = $this->upload->data();
                    $file_pasal = $uploadData['file_name'];
                } else {
                    $file_pasal = '';
                }
            } else {

                $file_pasal = '';
            }
            $data_post = array(
                'id_bunyi_pasal'  => '',
                'id_pasal'        => $this->input->post('id_pasal'),
                'point'           => $this->input->post('point'),
                'isi_bunyi_pasal' => trim($this->input->post('isi_bunyi_pasal')),
                'file'            => $file_pasal,
                'keterangan'      => $this->input->post('keterangan'),
            );
            $return_id = $this->general->save($data_post, "tb_bunyi_pasal");
            $response['info'] = " Create Data Success ";
            $response['retCode'] = 0; //Success
            print_r($data_post);
            exit(0);
        }
        echo json_encode($response);
    } else {
        exit('Access Denied');
    }
}

通过inspect输出的参数:

id_pasal:32分:a isi_bunyi_pasal:测试上传说明:测试上传提交:

单击按钮保存时,没有文件与方法发布一起发送。

jquery mysql ajax codeigniter datatables
1个回答
0
投票

默认情况下,AJAX不处理文件上传(至少没有一些帮助)。

你应该使用Dropzone

配置它以便它不会自动处理队列autoProcessQueue: false

在文档中查找sending事件。您可以附加表单值,如下所示:

myDropzone.on("sending", function(file, xhr, formData) {
  formData.append("name", "somevalue");
  formData.append("name2", "somevalue2");
});

或者看:Adding more data to Dropzone.js post

一般来说:

  1. 如果通过,请进行验证......
  2. 初始化dropzone插件并触发上传
  3. 在发送事件中附加表单值
  4. 打印您的成功或错误消息(可以使用400标头在后端触发dropzone错误)

祝好运!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.