One - One Code All

Blog Content

将后端返回的文件流转为图片路径直接展示图片

前端相关 WEB   2015-11-10 19:15:47

将后端返回的文件流转为图片路径直接展示图片


最简单直接的方法:直接在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标签



上一篇:APScheduler中两种调度器的区别及使用过程中要注意的问题
下一篇:postgresql修改字段长度

The minute you think of giving up, think of the reason why you held on so long.