2025-12-18 16:09:44 +08:00
|
|
|
import { downloadFile } from '@/api/system-boot/file'
|
|
|
|
|
// 下载文件
|
2025-12-22 15:13:43 +08:00
|
|
|
export const download = (urls: any) => {
|
2025-12-24 10:41:04 +08:00
|
|
|
console.log('下载',urls)
|
2025-12-18 16:09:44 +08:00
|
|
|
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])
|
2025-12-18 16:09:44 +08:00
|
|
|
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
|
|
|
|
|
}
|
2025-12-24 10:41:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// 预览文件
|
|
|
|
|
export const previewFile = async (urls: any) => {
|
|
|
|
|
console.log('预览',urls)
|
|
|
|
|
let url = ''
|
|
|
|
|
await 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'
|
|
|
|
|
: ''
|
|
|
|
|
})
|
|
|
|
|
url = window.URL.createObjectURL(blob)
|
|
|
|
|
})
|
|
|
|
|
console.log('url',url)
|
|
|
|
|
return url
|
|
|
|
|
}
|