后端返回文件流,前端展示图片
1、请求接口时,一定要加上 responseType:'blob',否则返回的格式不对
export function download(id) {
return request({
url: `/download?id=${id}`,
responseType:'blob',
method: 'get',
})
}
2、如果现要展示为图片,把返回的文件流转为路径
//res为返回的文件流,一定要写type, 返回的url 为 图片路径
let url =(window.URL || window.webkitURL).createObjectURL(new Blob([res], { type:'image/png'}));
3、如果要下载,利用document.createElement('a') 创建标签下载
let url =(window.URL || window.webkitURL).createObjectURL(new Blob([res], { type:'image/png'}));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', 'png');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);