fix: 修复点击下载只跳转不下载问题
This commit is contained in:
@@ -729,9 +729,45 @@ const downloadFile = async (url: string) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
window.open(url, '_blank')
|
// 获取文件信息
|
||||||
|
const fileItem = formData.value.fileUploads.find((i) => i.url === url)
|
||||||
|
const fileName = fileItem?.fileName || '下载文件'
|
||||||
|
|
||||||
|
// 使用 fetch 获取文件
|
||||||
|
const response = await fetch(url)
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件内容
|
||||||
|
const blob = await response.blob()
|
||||||
|
|
||||||
|
// 创建对象URL
|
||||||
|
const objectUrl = URL.createObjectURL(blob)
|
||||||
|
|
||||||
|
// 创建下载链接
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = objectUrl
|
||||||
|
link.download = fileName
|
||||||
|
link.style.display = 'none'
|
||||||
|
|
||||||
|
// 触发下载
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
|
||||||
|
// 清理
|
||||||
|
document.body.removeChild(link)
|
||||||
|
URL.revokeObjectURL(objectUrl)
|
||||||
|
|
||||||
|
message.success('文件下载成功')
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(`下载失败:${err.message}`)
|
console.error('Download error:', err)
|
||||||
|
// 如果 fetch 下载失败,回退到直接打开链接
|
||||||
|
try {
|
||||||
|
window.open(url, '_blank')
|
||||||
|
} catch (openError) {
|
||||||
|
message.error(`下载失败:${err.message}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user