前端将后台返回的数据流转为图片

var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",userUrl2 + 'user/getQrCode'+accessToken+"&name="+item.name+"&phone="+item.phoneNumber+"&company="+item.companyName,true);
xmlhttp.responseType = "arraybuffer";
xmlhttp.onload = function(){
// console.log(this);
var res=this.response;
if (this.status == 200) {
var code = 'data:image/png;base64,' + btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))
editMode.find('#edit-BelongCard').attr('src', code)
}
}
xmlhttp.send();



稍微改动过:
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",userUrl2 + 'user/getQrCode'+accessToken+"&name="+item.name+"&phone="+item.phoneNumber+"&company="+item.companyName,true);
xmlhttp.responseType = "arraybuffer";
xmlhttp.onload = function(){
// console.log(this);
var res=this.response;
if (this.status == 200) {
// var code = 'data:image/png;base64,' + btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))
var bytes = new Uint8Array(res);
var storeData = "";
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
storeData += String.fromCharCode(bytes[i]);
}
var code='data:image/png;base64,'+ window.btoa(storeData);
common.find('#detail-businessCardFront').attr('src', code);//专属凭证
}
}
xmlhttp.send();
posted @ 2020-04-03 11:09  诺克萨斯小刀  阅读(4431)  评论(0编辑  收藏  举报