<template>
<div class="group_insurance_order1" style="padding-top:100px;">
:auto-upload="false" 是否在选取文件后立即进行上传 false阻止自动上传 -- 且上传前和上传成功的事件都不会再触发 只会触发@change事件了
:http-request="uploadFile" 覆盖默认的上传行为,可以自定义上传的实现(this.$refs.uploadRef.submit() 会触发调用uploadFile函数)
-->
<el-upload
ref="uploadRef"
class="upload-demo"
action="#"
accept=".pdf"
:disabled="disabledUpload"
:auto-upload="false"
:on-change="changeFile"
:on-error="fileErr"
:on-exceed="handleExceed"
:file-list="fileList1"
:before-upload="beforeAvatarUpload"
:on-success="msgSuccessOne"
:data="fileData"
list-type="picture"
drag
:show-file-list="false"
:multiple="true"
:limit="1000">
<i class="el-icon-upload"></i>
<div class="el-upload__text" style="margin-top: -10px;line-height: 20px;">
将文件拖到此处,<em v-if="!disabledUpload"
>或点击上传(单个文件需小于100M,一次最多上传1000个pdf文件)</em
><em v-else>(文件正在上传中,请等待...)</em>
</div>
</el-upload>
<div>
<el-button
v-if="showPercent"
style="margin-left: 10px;"
size="small"
type="success"
@click="submitAbort"
>取消后续文件上传</el-button
>
</div>
<div style="color:orange;" v-if="showPercent">
上传过程请勿刷新浏览器和跳转其他页面...
</div>
<el-progress
v-if="showPercent"
:percentage="
Number(((percentNow * 100) / percentTotal).toFixed(0))
"></el-progress>
</div>
</template>
<script>
import axios from "axios";
export default {
data() {
return {
fileNum: "",
upFileList: "",
percentTotal: 0,
percentNow: 0,
showDesc: "",
showPercent: false,
time: null,
disabledUpload: false,
fileData: {},
fileList1: [],
};
},
activated: {
},
methods: {
handleExceed(files, fileList) {
console.log("当前限制一次性最多上传1000个文件", files, fileList);
this.$message.warning("当前限制一次性最多上传1000个文件");
},
changeFile(file, fileList) {
this.disabledUpload = true;
console.log("changeFile", file, fileList);
const isLt2M = file.size / 1024 / 1024 < 100;
if (!isLt2M) {
this.$message.warning("上传文件大小不能超过 100M");
}
if (!(file.name.indexOf(".pdf") > -1)) {
this.$message.warning("当前仅支持pdf格式的文件上传");
}
this.upFileList = [];
for (let x of fileList) {
if (
x.raw &&
x.name.indexOf(".pdf") > -1 &&
x.size / 1024 / 1024 < 100
) {
this.upFileList.push(x.raw);
this.percentTotal = this.upFileList.length;
this.percentNow = 0;
this.showPercent = false;
this.showDesc = "";
}
}
clearTimeout(this.time);
this.time = setTimeout(() => {
this.time = null;
console.log("防抖 高频触发后n秒内只会执行一次 再次触发重新计时");
this.fnBegin();
}, 1500);
},
fnBegin() {
console.log("此时change了所有文件 开始上传", this.upFileList);
this.submitUploadFun();
},
submitUploadFun() {
if (this.upFileList.length > 0) {
this.showPercent = true;
this.fileNum = new FormData();
this.fileNum.append("file", this.upFileList[0]);
this.fileNum.append("name", this.upFileList[0].name);
let _vm = this;
axios({
url: "/api/v1/accident/upload",
headers: {
"Content-Type": "multipart/form-data",
},
method: "post",
data: this.fileNum,
})
.then((res2) => {
this.percentNow = this.percentNow + 1;
this.upFileList.shift();
console.log("上传返回", res2);
if (res2.data.success) {
this.submitUploadFun();
} else {
_vm.$message({
message: res2.data.return_message || "上传失败",
type: "error",
});
this.showDesc = "上传结束,部分文件上传失败";
this.submitUploadFun();
}
})
.catch((error) => {
console.log(error);
_vm.$message({
message: error || "上传失败",
type: "error",
});
this.percentNow = this.percentNow + 1;
this.upFileList.shift();
this.showDesc = "上传结束,部分文件上传失败";
this.submitUploadFun();
});
} else {
this.disabledUpload = false;
this.showPercent = false;
this.upFileList = [];
this.$refs.uploadRef.clearFiles();
this.fileList1 = [];
if (this.percentNow == this.percentTotal && this.percentTotal) {
this.$message.success(
this.showDesc ? this.showDesc : "已全部上传成功!"
);
this.percentTotal = 0;
this.percentNow = 0;
this.showDesc = "";
} else if (
this.percentNow == this.percentTotal &&
this.percentTotal == 0
) {
this.$message.warning("请先选择文件!");
this.percentTotal = 0;
this.percentNow = 0;
} else {
this.$message.success("已部分上传成功,且取消后续文件上传!");
this.percentTotal = 0;
this.percentNow = 0;
}
return false;
}
},
submitAbort() {
this.showPercent = false;
this.upFileList = [];
},
fileErr(err, file, fileList) {
this.$message({
message: file.name + "上传失败",
type: "error",
});
},
beforeAvatarUpload(file) {
console.log("上传文件前", file);
},
msgSuccessOne(data, file, fileList) {
console.log("成功", file);
},
},
};
</script>