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