Files
admin-sjzx/src/utils/fileDownLoad.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

import { downloadFile } from '@/api/system-boot/file'
// 下载文件
2025-12-22 15:13:43 +08:00
export const download = (urls: any) => {
downloadFile({ filePath: urls }).then((res: any) => {
let blob = new Blob([res], {
type: urls.includes('.pdf')
? 'application/pdf'
: urls.includes('.zip')
? 'application/zip'
: urls.includes('.doc')
? 'application/msword'
: urls.includes('.docx')
? 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
: urls.includes('.xls')
? 'application/vnd.ms-excel'
: urls.includes('.xlsx')
? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
: urls.includes('.png')
? 'image/png'
: urls.includes('.jpeg')
? 'image/jpeg'
: urls.includes('.jpg')
? 'image/jpg'
: ''
})
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
2025-12-19 11:58:26 +08:00
let name = removeLastDotSuffix(urls.split('/')[2])
link.href = url
link.download = name
document.body.appendChild(link)
link.click()
link.remove()
})
2025-12-19 11:58:26 +08:00
}
function removeLastDotSuffix(str: string) {
// 找到最后一个 . 的位置
const lastDotIndex = str.lastIndexOf('.')
// 如果存在 .,截取到 . 之前的部分;否则返回原字符串
return lastDotIndex !== -1 ? str.slice(0, lastDotIndex) : str
}