前端將后台返回的數據流轉為圖片


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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM