|
@@ -1,25 +0,0 @@
|
|
|
-export const downloadFile = (url: string | Blob, fileName: string) => {
|
|
|
|
|
- const download = (blob: Blob, fileName: string) => {
|
|
|
|
|
- const a = document.createElement('a')
|
|
|
|
|
- document.body.appendChild(a)
|
|
|
|
|
- a.style.display = 'none'
|
|
|
|
|
- // 使用获取到的blob对象创建的url
|
|
|
|
|
- const url = window.URL.createObjectURL(blob)
|
|
|
|
|
- a.href = url
|
|
|
|
|
- // 指定下载的文件名
|
|
|
|
|
- a.download = fileName
|
|
|
|
|
- a.click()
|
|
|
|
|
- document.body.removeChild(a)
|
|
|
|
|
- // 移除blob对象的url
|
|
|
|
|
- window.URL.revokeObjectURL(url)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return new Promise<void>((resolve, reject) => {
|
|
|
|
|
- if (url instanceof Blob) {
|
|
|
|
|
- download(url, fileName)
|
|
|
|
|
- resolve()
|
|
|
|
|
- } else {
|
|
|
|
|
- fetch(url).then((res) => res.blob()).then((blob) => download(blob, fileName)).finally(() => resolve())
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
-}
|
|
|