Files
zkxw/web-admin/src/utils/download-attachment.ts
T
2026-05-19 12:03:08 +08:00

46 lines
1.3 KiB
TypeScript

import { ElMessage } from 'element-plus'
import { fetchAttachmentDownloadUrl } from '@/api/business'
export type AttachmentDownloadItem = {
fileUrl?: string
fileName?: string
}
function triggerAnchorDownload(href: string, fileName: string) {
const a = document.createElement('a')
a.href = href
a.download = fileName
a.rel = 'noopener'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
/** 附件下载:优先走后端预签名 URL,保证跨域场景下保留原始文件名与后缀。 */
export async function downloadAttachmentFile(
file: AttachmentDownloadItem,
options?: { fullFileUrl?: (url: string) => string }
) {
const fileUrl = String(file.fileUrl || '').trim()
if (!fileUrl) return
const fileName = String(file.fileName || '').trim() || fileUrl.split('/').pop() || 'download'
try {
const res = await fetchAttachmentDownloadUrl({ fileUrl, fileName })
if (res?.downloadUrl) {
triggerAnchorDownload(res.downloadUrl, fileName)
return
}
} catch {
// 预签名失败时回退直连
}
const resolveUrl = options?.fullFileUrl || ((url: string) => url)
const href = resolveUrl(fileUrl)
if (!href) {
ElMessage.error('无法下载附件')
return
}
triggerAnchorDownload(href, fileName)
}