我想将文件的名称命名为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 点:一 isi_bunyi_pasal:测试上传 keterangan:测试上传 提交:
单击按钮保存时,没有文件与方法发布一起发送。
答案 0 :(得分: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
所以一般: