1、当接口返回值类型是'application/json'时,处理报错信息
1 if (response.data.type && response.data.type == 'application/json') { 2 let reader = new FileReader(); 3 // 处理load事件。该事件在读取操作完成时触发 4 reader.onload = e => { 5 let res = JSON.parse(e.target.result); 6 _this.$message({ message: `${res.msg}`, type: "error", showClose: true }) 7 }; 8 reader.readAsText(response.data); 9 }
2、当接口返回是二进制流时,处理:
let blob = new Blob([response.data], { type: `application/x-download`
})
let href = window.URL.createObjectURL(blob)
let name = response.headers['content-disposition'].split(";")[1];
let fileName = name.split("=")[1];
let downloadElement = document.createElement('a')
downloadElement.href = href
downloadElement.download = decodeURI(fileName)
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement) // 下载完成除元素
3、完整代码
标签:axios,二进制,data,接口,href,let,downloadElement,type,response From: https://www.cnblogs.com/xia-chen/p/17864825.html