gpt4 book ai didi

javascript - Krajee Bootstrap 文件输入,捕获 AJAX 成功响应

转载 作者:可可西里 更新时间:2023-10-31 22:41:37 25 4
gpt4 key购买 nike

我正在使用 Krajee Bootstrap 文件输入插件通过 AJAX 调用执行上传。

这里是 Krajee 插件 AJAX 部分的链接:Krajee plugin AJAX

我使用的 JS 和 PHP (codeigniter) 代码如下:

JS:

<script>        
$("#file-upload").fileinput({
'allowedFileExtensions' : ['csv'],
'maxFileSize': 5120,
'maxFileCount': 1,
'uploadUrl': 'dashboard/uploader',
'elErrorContainer': '#errorBlock',
'uploadAsync': true,
'msgInvalidFileExtension': 'Invalid extension for file "{name}". Only "{extensions}" files are supported.',
'uploadExtraData': {csrf_token_name: $("input[name=csrf_token_name]").val()}
});
</script>

PHP:

public function uploader(){
$config['upload_path'] = './csv_uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5120';

$this->upload->initialize($config);
if (!$this->upload->do_upload("file-upload")){
$data['error'] = 'The following error occured : '.$this->upload->display_errors().'Click on "Remove" and try again!';
echo json_encode($data);
} else {
echo json_encode("success");
}
}

现在我从 PHP 得到了一个响应,无论它是错误还是成功,作为 JSON,我已经浏览了插件文档,但我仍然找不到如何捕获 AJAX 响应并根据我们的响应采取行动使用 ajax 成功函数在 jQuery 中执行:

success: function (response) {
//Deal with the server side "response" data.
},

我该怎么做?

最佳答案

您可以在此处查看演示 live demo

如果您希望触发成功事件,请记住设置uploadAsync false

示例代码:

JS

$("#input-id").fileinput({
showRemove:false,
showPreview: false,
uploadUrl: "../xxxx/xxxx/XXXXXX.php", // server upload action
uploadAsync: false,
uploadExtraData: function() {
return {
bdInteli: xxxx
};
}
});

// CATCH RESPONSE
$('#input-id').on('filebatchuploaderror', function(event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
});

$('#input-id').on('filebatchuploadsuccess', function(event, data, previewId, index) {
var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
alert (extra.bdInteli + " " + response.uploaded);
});

PHP

$nombre = $_FILES["ficheroExcel"]["name"];
$bdInteli = $_POST['bdInteli'];
if (move_uploaded_file($_FILES["ficheroExcel"]["tmp_name"], $nombre) ){
$output = array('uploaded' => 'OK' );
} else {
$output = array('uploaded' => 'ERROR' );
}
echo json_encode($output);

关于javascript - Krajee Bootstrap 文件输入,捕获 AJAX 成功响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29626410/

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