From 8fdb1078ae5afe2f2ad6e77e60b9a85aa99b61ac Mon Sep 17 00:00:00 2001 From: hr1201 <568127468@qq.com> Date: Tue, 26 Nov 2024 10:52:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E6=84=8F=EF=BC=9A=E6=96=B0=E5=A2=9Efi?= =?UTF-8?q?lesaver=EF=BC=8C=E9=9C=80=E8=A6=81=E6=9B=B4=E6=96=B0=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=9B=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 ++ src/api/attachment/index.ts | 6 +++--- src/views/attachment/index.vue | 32 +++++++++++++++++--------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 554989b..7a2b464 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "echarts-gl": "^2.0.9", "echarts-wordcloud": "^2.1.0", "element-plus": "^2.6.1", + "file-saver": "^2.0.5", "js-cookie": "^3.0.5", "js-table2excel": "^1.1.2", "jsplumb": "^2.15.6", @@ -40,6 +41,7 @@ "vue-router": "^4.3.0" }, "devDependencies": { + "@types/file-saver": "^2.0.7", "@types/node": "^20.11.28", "@types/nprogress": "^0.2.3", "@types/sortablejs": "^1.15.8", diff --git a/src/api/attachment/index.ts b/src/api/attachment/index.ts index ea69638..cfd2a73 100644 --- a/src/api/attachment/index.ts +++ b/src/api/attachment/index.ts @@ -26,11 +26,11 @@ export function attachmentApi() { method: 'get', }); }, - downloadFiles: (ids: object) => { + downloadFiles: (ids: String) => { return request({ - url: baseUrlHost + '/enAttachment/uploadByType', + url: baseUrlHost + `/enAttachment/downloadFiles/${ids}`, method: 'get', - ids, + responseType: 'blob', }) } }; diff --git a/src/views/attachment/index.vue b/src/views/attachment/index.vue index 52ac4f4..3b91673 100644 --- a/src/views/attachment/index.vue +++ b/src/views/attachment/index.vue @@ -35,8 +35,13 @@ 批量下载 - + @@ -72,6 +77,7 @@ import { articleApi } from '/@/api/article'; import { attachmentApi } from '/@/api/attachment'; import { ElMessage, TableColumnCtx } from 'element-plus'; import { useRouter } from 'vue-router'; +import FileSaver from 'file-saver'; const router = useRouter(); @@ -94,15 +100,15 @@ const state = reactive({ // 模块列表 interface Module { - id: number; - moduleName: string; + id: number; + moduleName: string; } const moduleList = ref([]); // 标签列表 interface Label { - name: string; + name: string; } const labelList = ref([]); @@ -135,21 +141,17 @@ const handleSelectionChange = (selection: any[]) => { }; // 批量下载 -const downloadSelected = () => { +const downloadSelected = async () => { if (!state.selectedRows.length) { ElMessage.warning('请先选择附件!'); return; } - state.selectedRows.forEach((file) => { - // 假设提供了 API 或工具进行文件下载 - // 示例:使用 Blob URL 或直接调用下载 API - console.log(`下载文件: ${file.id}`); - }); - // 需要传给后端ids的数据 - const ids = state.selectedRows.map((item) => item.id); - console.log(ids); - atcmApi.downloadFiles(ids); + const ids = state.selectedRows.map((item) => item.id).join(','); + let res = await atcmApi.downloadFiles(ids); + + const blob = new Blob([res], { type: 'application/zip' }); + FileSaver.saveAs(blob, "attachment.zip"); }; const getModuleList = async () => {