gpt4 book ai didi

javascript - 您没有选择要上传的文件。 ajax 代码点火器

转载 作者:行者123 更新时间:2023-12-03 02:37:42 25 4
gpt4 key购买 nike

型号:

public function insertSuratMasuk($filename)
{
$data = array('nomor_surat' => $this->input->post('nosurat'),
'tgl_kirim' => $this->input->post('tglkirim'),
'tgl_terima' => $this->input->post('tglterima'),
'pengirim' => $this->input->post('pengirim'),
'penerima' => $this->input->post('penerima'),
'perihal' => $this->input->post('perihal'),
'file_surat' => $filename,
'status' => 'proses'
);

$this->db->insert('surat_masuk', $data);

if($this->db->affected_rows() == 0){
return false;
}

return true;
}

Controller :

public function addSuratMasuk()
{
$config['allowed_types'] = 'pdf|doc|docx';
$config['upload_path'] = './uploads/';
$config['max_size'] = 4000;

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

$r = array('status' => true, 'error' => '');

if($this->upload->do_upload('suratMasuk')){
$upload = $this->upload->data();
$filename = $upload['file_name'];
if($this->surat_masuk_m->insertSuratMasuk($filename) == true){
echo json_encode($r);
} else {
$r['status'] = false;
echo json_encode($r);
}
} else {
$r['error'] = $this->upload->display_errors();
$r['status'] = false;
echo json_encode($r);
}
}

查看:

<form class="form-horizontal" enctype="multipart/form-data" id="tambahSuratMasuk">
<div class="modal-body form">
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Nomor Surat</label>
<div class="col-md-9">
<input type="text" name="nosurat" class="form-control" placeholder="Nomor Surat" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Tgl. Kirim</label>
<div class="col-md-9">
<input type="date" name="tglkirim" class="form-control" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Tgl. Terima</label>
<div class="col-md-9">
<input type="date" name="tglterima" class="form-control" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Pengirim</label>
<div class="col-md-9">
<input type="text" name="pengirim" class="form-control" placeholder="Pengirim" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Penerima</label>
<div class="col-md-9">
<input type="text" name="penerima" class="form-control" placeholder="Penerima" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Perihal</label>
<div class="col-md-9">
<input type="text" name="perihal" class="form-control" placeholder="Perihal" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Unggah Surat (*.pdf)</label>
<div class="col-md-9">
<input type="file" name="suratMasuk" class="form-control" required>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" name="close" id="buttonClose" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
<input type="submit" name="submit" value="Save" class="btn btn-primary" onclick="insertSuratMasuk(); ">
</form>

Javascript:

function insertSuratMasuk() {
event.preventDefault();
data = $('#tambahSuratMasuk').serialize();
$.ajax({
url: '<?php echo base_url('index.php/surat_masuk/addSuratMasuk'); ?>',
type: 'POST',
dataType: 'json',
data: data,
success: function (r) {
if(r.error == ''){
if(r.status == true){
swal('Insert data success', '', 'success');
} else if (r.status == false) {
swal('Insert data failed', '', 'danger')
}
} else {
swal('Insert data failed', r.error, 'warning')
}
}
})
}

当我想编写如何上传文件的代码时,出现错误,但是当要上传文件时,出现问题,并且出现此错误:

you did not select a file to upload

我不知道为什么,我认为我的所有代码都很清楚。我很困惑。有人可以帮助我吗?我非常希望有人能修复我的代码。谢谢大家。

最佳答案

您不能直接使用ajax上传文件数据,但您可以使用FormData()达到相同目的:

$.ajax({
...
data : new FormData($('#tambahSuratMasuk')),
...
})

关于javascript - 您没有选择要上传的文件。 ajax 代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com