我们都知道,文件下载的一种实现方案就是后端返回文件流,然后前端进行生成a标签并触发点击来下载。但是在火狐浏览器的时候,需要注意一些兼容性问题。原因是火狐的同源策略。官方说明:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes
代码如下:
1 download(row) { 2 downloadFile({ id: row.fileId }).then(res => { 3 // window.open(URL.createObjectURL(res.data)) 4 const blob = new Blob([res.data], { type: row.fileType }) 5 // var a = document.createElement(‘a‘)兼容火狐 6 const a = document.getElementById(‘downFA‘) 7 a.href = URL.createObjectURL(blob) 8 a.download = row.fileName 9 a.click() 10 // URL.revokeObjectUrl(a.href) 11 }) 12 }
由于是异步的,mouted时有时会获取不到?改成created也会有时获取不到?使用watch是一种思路。
原文:https://www.cnblogs.com/ljwsyt/p/11428633.html