效果图
template部分
按钮
<el-row :gutter="10" class="mb12">
<el-col :span="1.5">
<ComBtn
@click="multipleImport"
:btnName="'批量导入'"
></ComBtn>
</el-col>
<el-col :span="1.5">
<exportBtn @click="multipleExport"></exportBtn>
</el-col>
<el-col :span="1.5">
<ComBtn
@click="imageExport"
:btnName="'导入图片'"
></ComBtn>
</el-col></el-row>
对话框
<el-dialog
v-model="importOpen"
width="500px"
append-to-body
title="导入信息"
:before-close="closeImp"
>
<div class="el-upload__tip">
{{tips=='只能上传xlsx/xls类型的文件'?'':tips}}
<div class="el-upload__tip1" v-if="tips == '只能上传xlsx/xls类型的文件'">
<p>
1、只能上传
<i class="xls">xlsx/xls</i> 类型的文件'
</p>
2、{{ excelTip }}
<a
href="../../../../public/学生信息模板.xlsx"
download="学生信息模板.xlsx"
class="red"
>下载Excel模板</a>
</div>
</div>
<table>
<thead>
<td class="head" style="width: 200px;">文件名</td>
<td class="head" style="width: 200px;">大小/kb</td>
<td class="head" style="width: 80px;">状态</td>
</thead>
<tbody>
<tr>
<td>{{filename}}</td>
<td>{{filesize}}</td>
<td>{{filestatus}}</td>
</tr>
</tbody>
</table>
<el-upload
drag
:before-upload="beforeUpload"
ref="uploadrefss"
:http-request="uploadHandler"
:show-file-list="false"
class="uploa"
>
<div class="el-upload__text">
将文件拖拽到此区域或者
<em>点击上传</em>
</div>
</el-upload>
</el-dialog>
js部分
// TODO导入xlsx
const text = ref("");
const tips = ref("");
const excelTip = ref("");
const xORi = ref();
const importOpen = ref(false);
const uploadrefss = ref();
const multipleImport = () => {
xORi.value = "excel";
importOpen.value = true;
tips.value = "只能上传xlsx/xls类型的文件";
excelTip.value = "上传文件前,请先";
if (uploadrefss.value) {
uploadrefss.value.clearFiles();
}
};
const filename = ref("");
const filesize = ref("");
const filestatus = ref("");
// 文件上传之前的操作
function beforeUpload(params) {
filename.value = params.name;
filesize.value = (params.size / 1024).toFixed(2);
filestatus.value = "上传中";
/* 在上传之前校验的文件格式 */
if (xORi.value == "excel") {
if (!(params.name.endsWith(".xlsx") || params.name.endsWith(".xls"))) {
ElMessage({
type: "warning",
message: "请选择正确的格式文件上传",
});
return false;
}
} else {
if (!(params.name.endsWith(".rar") || params.name.endsWith(".zip"))) {
ElMessage({
type: "warning",
message: "请选择正确的格式文件上传",
});
return false;
}
}
}
function uploadHandler(val) {
let fd = new FormData();
if (xORi.value == "excel") {
fd.append("excelFile", val.file);
importStuInfoList(fd).then((res) => {
});
} else {
fd.append("zipFile", val.file);
imageStuInfoList(fd).then((res) => {
});
}
filestatus.value = "上传成功";
requestAPI();
}
function closeImp() {
importOpen.value = false;
filename.value = "";
filesize.value = "";
filestatus.value = "";
}
// TODO导入图片
function imageExport() {
xORi.value = "image";
importOpen.value = true;
tips.value = "上传的图片以学生的学号或身份证命名(例如:学号.jpg),并进行压缩";
uploadrefss.value.clearFiles();
}