场景:文件下载时,前端通过axios请求后台接口,同时将文件的“半链接”传给该接口,此时后台会重定到文件下载的链接,如果文件过大时,部分浏览器则会出现上述报错,导致无法下载
解决方案:前端通过fetch请求该接口,然后获取到对应的重定向链接,然后通过前端下载即可
注意:在使用fetch请求时,要添加token
下边直接上代码
问题代码:
// 下载全部模板
const downloadModel = async () => {
const url = '/common/url'
if (url ) {
tempDownload(url , '全部模板.zip')
} else {
ElMessage.warning('模板不存在!')
}
}
// 前端下载方法
const tempDownload = async (url: string, fileName?: string) => {
// 此处的接口,后台会发生重定向,导致部分浏览器在重定向时,产生报错
const blob = await downloadPermission({ url })
const a = document.createElement('a')
a.style.display = 'none'
const href = window.URL.createObjectURL(blob)
a.href =