将后端返回的文件流转为图片路径直接展示图片
最简单直接的方法:直接在src中写地址。
var fullurl = 'http://192.168.2.1:8080/getimage?name=one'; image = document.getElementById("imageid"); image.src = fullurl;
其他方案:
imgHerf = window.URL.createObjectURL(data)
后台返回图片流,前端预览图片,解决方案
blob转base64
'data:image/png;base64,' + btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))
注意:请求数据类型,responseType需要指定为arrayBuffer
blob转url
URL.createObjectURL(blob)
注意:只能用于img标签