gpt4 book ai didi

javascript - Codeigniter 3 - Ajax 文件上传(您没有选择要上传的文件)

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:23 24 4
gpt4 key购买 nike

Controller :

function post()
{
if(!$this->input->is_ajax_request())
{
show_404();
exit;
}

$data['result'] = false;

$config['upload_path'] = base_url().'userfiles/customer';
$config['allowed_types'] = 'pdf';
$config['max_size'] = 100000;

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

$file = $this->input->post('userfile');

if ( ! $this->upload->do_upload($file))
{
$data['result'] = true;
$error = array('error' => $this->upload->display_errors());

$data['error'] = $error;
}
else
{
$data['result'] = true;
$data_upload = array('upload_data' => $this->upload->data());

$data['success'] = $data_upload;
}

echo json_encode($data);
}

查看:

<div class="form">
<div class="form-group">
<label class="form-label" for="package">Package :</label>
<select class="package form-control" id="package" name="package">
<option value="none">Please select..</option>
<?php foreach($package as $row) { ?>
<option value="<?php echo $row['id_order_package'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<div class="error" id="msg_package"></div>
</div>
<div class="form-group">
<label class="form-label" for="price">Price ( Rp ) :</label>
<input type="text" class="form-control" name="price" id="price" placeholder="" disabled>
<div class="error" id="msg_price"></div>
</div>
<div class="form-group">
<label class="form-label" for="days">Days :</label>
<input type="text" class="form-control" name="days" id="days" placeholder="" disabled>
<div class="error" id="msg_days"></div>
</div>
<div class="form-group">
<label class="form-label" for="userfile">File :</label>
<input type="file" id="userfile" name="userfile" class="form-control">
<div class="error" id="msg_userfile"></div>
</div>
<div class="form-group">
<label class="form-label" for="description">Description :</label>
<textarea class="form-control" rows="5" name="description" id="description"></textarea>
<div class="error" id="msg_description"></div>
</div>
<div class="form-group center">
<div class="btn btn-primary">ORDER</div>
</div>
</div>

Javascript:

$('.btn').click(function(e){
$('#msg_package').html(' ');
$('#msg_userfile').html(' ');
$('#msg_description').html(' ');

if($('#package').val() == 'none') {
$('#msg_package').html('Please select package');
} else if($('#userfile').val() == '') {
$('#msg_userfile').html('This field is required');
} else if($('#description').val() == '') {
$('#msg_description').html('This field is required');
} else {

$.ajax({
url : '<?php echo base_url() ?>account_order/add/post',
type : 'POST',
dataType: 'json',
mimeType:"multipart/form-data",
processData: false,
data : { userfile: $('#userfile')[0].files , price: $('#price').val() , days: $('#days').val() },
success: function( data ) {

if(!data['result']){

}else{
alert(data['error']);
}
}
});
}
});

但是,我得到一个错误

{"result":true,"error":{"error":"<p>You did not select a file to upload.<\/p>"}}

我试图从 ajax 发布数据和文件,但它不起作用。我的问题是如何修复该错误?为什么controller读不到userfile的内容?

谢谢

最佳答案

尝试将您的输入放入表单并提供表单 id='your_form_id' 然后通过这种方式在 javascript 中发送您的数据:

var data = new FormData(document.getElementById("your_form_id"));

然后替换这一行 data : { userfile: $('#userfile')[0].files , price: $('#price').val() , days: $('#days' ).val() },

用这个data: data,

也在 data: data, 之后设置:

async: false,
processData: false,
contentType: false,

关于javascript - Codeigniter 3 - Ajax 文件上传(您没有选择要上传的文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151367/

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