download file

 

在页面中插入 a 标签方式下载文件,避免新开窗口导致的闪烁

 1 function downloadFile(file) {
 2     fetch(file.fileUrl)
 3         .then(res => res.blob())
 4         .then(blob => {
 5             const a = document.createElement('a')
 6             a.style.display = 'none'
 7             document.body.appendChild(a)
 8             const url = window.URL.createObjectURL(blob)
 9             a.href = url
10             a.download = file.fileName
11             a.click()
12             document.body.removeChild(a)
13             window.URL.revokeObjectURL(url)
14         })
15         .catch(error => {
16             console.error(error)
17             window.open(file.fileUrl)
18         })
19 }

 

posted @ 2022-11-17 14:53  yuhui_yin  阅读(85)  评论(0编辑  收藏  举报