axios({
method: 'post',
url: '/api/Api/DownloadBizObjectFile',
headers: {
"Content-Type": "application/json",
"EngineCode": "j371gy1q1ioeveq2y05l4gjz3",
"EngineSecret": "IgMlZo+KOs8FthII3KnobUoynILH5ELJVqcHNZcs1G2mwrkofR1THw=="
},
responseType: 'arraybuffer',
data: {
"attachmentId": this.logoId
}
}).then(res=>{
const bufferUrl = btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''));
this.verifyCode = 'data:image/png;base64,' + bufferUrl;
}).catch(e=>{
throw e;
})
this.logoId为此接口之前调用的第一个接口返回拿到的图片标识id,在这里作为参数;
EngineCode和EngineSecret为公司内部公共接口的免登身份校验。
思路:网上查了很多资料说是转成blob格式的,试了很久都不行,在这里注意的是跟postman正常请求成功要做对比,有时候在postman中试了可以,但是未必在代码里你可以写正确。
在此,responseType设置为arraybuffer,然后在res返回回调中用btoa和Uint8Array方法将二进制流转变成url地址,注意src后面要注明’data:image/png;base64,’,后面拼接转化的url图片地址
下面为postman和页面真实请求对比图:(可能会产生点灵感)