diff --git a/snowy-admin-web/src/utils/downloadUtil.js b/snowy-admin-web/src/utils/downloadUtil.js index 5126a8e4..19982d72 100644 --- a/snowy-admin-web/src/utils/downloadUtil.js +++ b/snowy-admin-web/src/utils/downloadUtil.js @@ -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对象 + } } }