以下是通过axios进行请求
其他东西不用修改,但是需要包相应方式设为blob
export function getBlob(){
return axios({
url: '/flow/123',
method: 'post',
data,
responseType: 'blob'
})
}
在返回参数读取完成后(onloadend)转换文件的内容(fileReader.result),通过是否出现异常判断文件类型
如果不是文件流可在try中给出提示等操作
如果是文件流可以在catch中进行下载等操作
let data = {
test: 1
}
getBlob(data).then(res => {
const fileReader = new FileReader()
fileReader.onloadend = () => {
try{
const jsonData = JSON.parse(fileReader.result) // 说明是普通对象数据,转换失败
}catch(err){ // 解析对象失败,说明是正常的文件流
// 下载文件
const blob = new Blob([res], {
type: res.type
})
const picSrc = window.URL.createObjectURL(blob)
window.open(picSrc)
}
}
})
注:在相应种类中一定要设置成 responseType: 'blob',如果设置为 responseType: 'arraybuffer', 在请求返回后使用 new FileReader(),来处理判断后端是否返回的是一个文件流的时候就会报错