【更新】统一下载blob功能加入判断

pull/94/head
小诺 2023-03-14 23:54:57 +08:00 committed by 俞宝山
parent d18eb01292
commit 747b6973be
1 changed files with 22 additions and 10 deletions

View File

@ -8,18 +8,30 @@
* 5.不可二次分发开源参与同类竞品如有想法可联系团队xiaonuobase@qq.com商议合作
* 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip
*/
import { message } from 'ant-design-vue'
export default {
// 对下载的流进行处理,直接从浏览器下载下来
resultDownload (res) {
const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
const contentDisposition = res.headers['content-disposition']
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
const $link = document.createElement('a')
$link.href = URL.createObjectURL(blob)
$link.download = decodeURIComponent(patt.exec(contentDisposition)[1])
$link.click()
document.body.appendChild($link)
document.body.removeChild($link) // 下载完成移除元素
window.URL.revokeObjectURL($link.href) // 释放掉blob对象
if (res.data.type === 'application/json') {
// 错误以及无权限
const reader = new FileReader(res.data)
reader.readAsText(res.data)
reader.onload = () => {
const result = JSON.parse(reader.result)
message.error(result.msg)
}
} else {
const blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
const contentDisposition = res.headers['content-disposition']
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
const $link = document.createElement('a')
$link.href = URL.createObjectURL(blob)
$link.download = decodeURIComponent(patt.exec(contentDisposition)[1])
$link.click()
document.body.appendChild($link)
document.body.removeChild($link) // 下载完成移除元素
window.URL.revokeObjectURL($link.href) // 释放掉blob对象
}
}
}