首页 > 其他分享 >download file

download file

时间:2022-11-17 15:01:28浏览次数:29  
标签:window blob file error download document

 

在页面中插入 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 }

 

标签:window,blob,file,error,download,document
From: https://www.cnblogs.com/veinyin/p/16899499.html

相关文章