From e02d9e80005ba7b37cd6bec498844dd9491ef292 Mon Sep 17 00:00:00 2001 From: lSun <15893999301@qq.com> Date: Wed, 17 Sep 2025 10:53:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E6=9C=8D=E5=8A=A1=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0-=E5=AE=A3=E4=BC=A0=E7=89=A9=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../publicService/proMaterials/index.vue | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/views/publicService/proMaterials/index.vue b/src/views/publicService/proMaterials/index.vue index 18781d3..2890765 100644 --- a/src/views/publicService/proMaterials/index.vue +++ b/src/views/publicService/proMaterials/index.vue @@ -95,6 +95,7 @@ import DialogModel from '@/components/DialogModel/index' import {getMaterialListAPI} from '@/api/publicService/proMaterials' import { encryptCBCTime } from '@/utils/aes' import useBase64 from '@/utils/base64Utils.js' +import axios from 'axios' export default { name: 'ProMaterials', @@ -182,42 +183,32 @@ export default { } }, // 下载 - handleDownload(service) { - let url = service.filePath; + // 下载 + async handleDownload(service) { + try { + const response = await axios({ + method: 'get', + url: process.env.VUE_APP_BASE_API + '/system/files/download', + params: { + filePath: service.fileDownloadPath + }, + responseType: 'blob' // 重要:指定响应类型为blob + }); - // 确保 URL 是完整的绝对路径 - if (!url.startsWith('http://') && !url.startsWith('https://')) { - if (url.startsWith('www.')) { - url = 'https://' + url; - } else { - url = 'https://' + url; - } + // 创建下载链接 + const url = window.URL.createObjectURL(new Blob([response.data])); + const link = document.createElement('a'); + link.href = url; + link.setAttribute('download', service.fileName || 'file'); // 设置下载文件名 + document.body.appendChild(link); + link.click(); + link.remove(); + window.URL.revokeObjectURL(url); // 清理URL对象 + + this.$message.success('下载成功'); + } catch (error) { + this.$message.error('下载失败'); } - - // 创建临时 a 标签来触发下载 - const link = document.createElement('a'); - link.href = url; - link.download = ''; // 可指定文件名,如 'filename.zip';空字符串表示使用服务器原文件名 - link.target = '_blank'; // 兼容某些浏览器 - link.style.display = 'none'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); // 清理 - alert('下载成功') - - /*// 确保 URL 是完整的绝对路径 - let url = service.filePath - // 如果 URL 不是以 http:// 或 https:// 开头,则添加 https:// - if (!url.startsWith('http://') && !url.startsWith('https://')) { - // 如果 URL 以 www. 开头,直接添加 https:// - if (url.startsWith('www.')) { - url = 'https://' + url - } else { - // 其他情况,添加 https:// - url = 'https://' + url - } - } - window.open(url, '_blank')*/ }, // 鼠标悬浮 handleCardHover(serviceId) {