假设接口返回的数据为 res。数据为文件流
// blob方案
let blob = new Blob([res], {type: "img/jpeg"});
let url = (URL || webkitURL).createObjectURL(blob);
let imgDiv = document.getElementById('secret');
imgDiv.src = url;//这个可以
// imgDiv.setAttribute('src',url)//这个写法也可以
imgDiv.onload = function (e) {
URL.revokeObjectURL(url)
}
页面元素为img
<img id={'secret'} />
网络请求中需要注意reponseType
export async function toGetSecret(id) {
return request(`/api/account/${id}/code`, {method: 'GET',responseType:'blob'});
}