From 1a4b6f01bc264a89906dd361fe70c83dff7fd28c Mon Sep 17 00:00:00 2001 From: buaixuexideshitongxue <2936013465@qq.com> Date: Wed, 17 Dec 2025 18:05:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=81=9A?= =?UTF-8?q?=E9=99=90=E5=88=B6=EF=BC=8C=E4=B8=8B=E8=BD=BD=E5=81=9A=E4=B8=AA?= =?UTF-8?q?=E6=80=A7=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/file/list.vue | 37 +++++++++++++++++++++++++++++++++- src/components/file/upload.vue | 10 +++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/components/file/list.vue b/src/components/file/list.vue index 0357dd4..a1f76b2 100644 --- a/src/components/file/list.vue +++ b/src/components/file/list.vue @@ -231,6 +231,7 @@ import "@vue-office/docx/lib/index.css"; import "@vue-office/excel/lib/index.css"; import VueOfficeDocx from "@vue-office/docx"; import VueOfficeExcel from "@vue-office/excel"; +import {ElMessage} from "element-plus"; const props = defineProps({ files: { @@ -257,9 +258,43 @@ let initialY = 0; const fileRrror = ref(false); const tableDatas = ref([]) function filePreview(file) { + if (!file?.filePath) { + ElMessage?.warning?.('文件路径不存在,无法下载') + return + } + const url = `${BASE_PATH}/file/stream/${file.filePath}` + const type = getFileType(file.fileName) + // 1. PDF:新窗口打开 + if (type === FileType.PDF) { + window.open(url, '_blank') + return + } +// 2. Word / Excel:直接下载(不打开窗口) + if ( + type === FileType.WORD || + type === FileType.EXCEL + ) { + const a = document.createElement('a') + a.href = url + a.download = file.fileName || '' + document.body.appendChild(a) + a.click() + document.body.removeChild(a) + return + } - preview.value = true; + // 3. 其他类型:默认直接下载 + const a = document.createElement('a') + a.href = url + a.download = file.fileName || '' + document.body.appendChild(a) + a.click() + document.body.removeChild(a) + + activeIndex.value = index; activeFile.value = file; + console.log(activeFile.value) + preview.value = true; rotate.value = 0; scale.value = 1; translateX.value = 0; diff --git a/src/components/file/upload.vue b/src/components/file/upload.vue index e2a157f..b365db7 100644 --- a/src/components/file/upload.vue +++ b/src/components/file/upload.vue @@ -73,6 +73,16 @@ watch(files, () => { },{immediate:true}); function beforeUpload(file) { + const allowExt = ['doc', 'docx', 'pdf', 'xls', 'xlsx'] + const name = (file?.name || '').toLowerCase() + const ext = name.includes('.') ? name.split('.').pop() : '' + + if (!allowExt.includes(ext)) { + feedback.msgWarning( + '只能上传 Word / PDF / Excel 文件(.doc .docx .pdf .xls .xlsx)' + ) + return false + } // 文件大小限制 1024 * 1024 * 100 (100MB) if (file.size >= 104857600) { feedback.msgError('文件大小不能超过100MB');