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');