vue项目接口返回json或者文件流如何处理
一、接口请求设置为 responseType: ‘blob’二、export upload = data => {retrun request({url:' ',method:'post',resopnseType:'blob',data})}三、let file = this.fileCopy [0].raw;var formData = new FormData();formData.a
·
一、
接口请求设置为 responseType: ‘blob’
二、
export upload = data => {
retrun request({
url:' ',
method:'post',
resopnseType:'blob',
data
})
}
let file = this.fileCopy [0].raw;
var formData = new FormData();
formData.append('file',file)
upload(formData ).then( async res => {
if (res.data.type === 'application/json'){
//Blob.text()返回一个promise且包含blob所有内容的UTF-8格式的 USVString。
let text = await res.data.text();
let jsonText = await JSON.parse(text)
}else{
const blob = new Blob([res.data],{type:'application/octet-stream;charset=utf-8'})
let link = document.createElement('a')
link.style.display = 'none'
let objectUrl = URL.createObjectURL(blob)
link.href = objectUrl
link.download = '模板.xlsx'
link.click()
URL.revokeObjectUrl(objectUrl)
}
})
更多推荐
已为社区贡献2条内容
所有评论(0)