How to convert a byte array into an image?

后端 未结 8 2114
一向
一向 2020-12-01 05:51

Using Javascript, I\'m making an AJAX call to a WCF service, and it is returning a byte array. How can I convert that to an image and display it on the web page?

8条回答
  •  长情又很酷
    2020-12-01 06:33

    Late to the party but if your response looks like

    [137,80,78,71,13,10,26,10,0,...]
    

    you can use this:

    
    
    var imgsrc = "data:image/png;base64," + btoa(String.fromCharCode.apply(null, new Uint8Array([137,80,78,71,13,10,26,10,0,...])));
    
    document.getElementById('image').src = imgsrc;
    

提交回复
热议问题