今天我在用bootstrap-fileinput的时候,前台调试的时候,一直提示如下错误:
jquery.min.js:2 Uncaught ReferenceError: FileInput is not defined
at HTMLDocument.<anonymous> (?action=picture&type=add:88)
at e (jquery.min.js:2)
at t (jquery.min.js:2)
中文翻译
未捕获的ReferenceError:未定义FileInput
原因:我的文件引入顺序有问题。
解决方法
为了让大家更好的理解,我还是把我的简单示例代码给大家发一下,这样大家就能明白多了。
前端代码:index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎来到付杰博客:bootstrap-fileinput 文件上传</title>
<script src="/bootstrap-fileinput/jquery-3.5.1.min.js?x61917" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/bootstrap-fileinput/bootstrap-3.3.7/css/bootstrap.min.css?x61917" crossorigin="anonymous">
<script src="/bootstrap-fileinput/bootstrap-3.3.7/js/bootstrap.min.js?x61917" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/bootstrap-fileinput/bootstrap-fileinput/css/fileinput.min.css?x61917" crossorigin="anonymous">
<!-- 如果你想在最初的预览中排序/重新排列需要引入sortable.min.js 它必须在fileinput.min.js之前引入 -->
<script src="/bootstrap-fileinput/bootstrap-fileinput/js/plugins/sortable.min.js?x61917" type="text/javascript"></script>
<!-- 如果你想在HTML文件预览中净化HTML内容则要引入purify.min.js is 它必须在fileinput.min.js之前引入 -->
<script src="/bootstrap-fileinput/bootstrap-fileinput/js/plugins/purify.min.js?x61917" type="text/javascript"></script>
<script src="/bootstrap-fileinput/bootstrap-fileinput/js/plugins/piexif.min.js?x61917" type="text/javascript"></script>
<!-- 主要的 fileinput 插件库 -->
<script src="/bootstrap-fileinput/bootstrap-fileinput/js/fileinput.min.js?x61917"></script>
<!-- 可选,如果你需要像font awesome 这样的主题,就像下面的代码一样引入它 -->
<!-- 可选,如果你需要转换语言或翻译,就包含这个库 -->
<script src="/bootstrap-fileinput/bootstrap-fileinput/js/locales/zh.js?x61917"></script>
</head>
<body>
<div class="container my-4">
<form enctype="multipart/form-data">
<div class="file-loading">
<input id="input-703" name="kartik-input-703[]" type="file" multiple>
</div>
<button type="submit" class="btn btn-primary">提交</button>
<button type="reset" class="btn btn-outline-secondary">重置</button>
</form>
</div>
<script>
$(document).ready(function() {
$("#input-703").fileinput({
theme: 'fas',
language: 'zh',
uploadUrl: "/bootstrap-fileinput/upload.php", // 服务器端上传处理程序
minFileCount: 1, //最小上传文件数为1
maxFileCount: 5, //最大上传文件数为5
multiple:true //异步上传
})
});
</script>
</body>
</html>
后端代码:upload.php
<?php
//header('Content-Type:application/json; charset=utf-8');
// ...
// 处理ajax上传并返回JSON响应的服务器代码。
// 你的服务器方法必须返回一个包含`initialPreview`、`initialPreviewConfig`和`append`的JSON对象。
// 一个PHP服务器代码示例如下:
// ...
//var_dump($_FILES);
//var_dump($_GET);
//var_dump($_POST);
if ($_FILES["kartik-input-703"]["name"]) {
$name=$_FILES["kartik-input-703"]["name"][0];
$target1 = time().$name;
$target = "./".$target1;
// echo $target;
$file = move_uploaded_file($_FILES['kartik-input-703']['tmp_name'][0], $target);
// echo $imagurl;
// echo '<br/>';
if ($file){
$key = $target1;
$url = 'http://127.0.0.1/bootstrap-fileinput/delete.php'; //你删除这个文件的服务器图片方法URL
$strjson = json_encode([
"initialPreview" =>[
"http://127.0.0.1/bootstrap-fileinput/$target1"
],
"initialPreviewConfig" => [
['caption' => "{$key}", 'size' => 627392, 'width' => '120px', 'url' => $url, 'key' => $key],
],
"append" => true // 是否把这些配置加入`initialPreview`。
// 如果设置为`false`,它会重载初始预览。
// 如果设置为`true`,它会加入初始预览之中。
// 如果这个属性没有被设置或者没有传出,它会默认为`true`。
],320);
echo $strjson;
}else {
echo json_encode(['error'=>'上传失败!']);
}
}
最终效果:
选择图片并点击上传时,如下图:
上传成功后效果如下图: