将字节数组转换为angular6中的图像

convert byte array to image in angular6

我已经从服务器发送了字节数组中的图像文件。现在我必须将其转换为 jpeg 文件并将其显示在网页中。

代码:

app.get('/getPhoto/:hash',function(req, res){
    console.log(req.params.hash);
    invoke = require('/Users/sanjeev.natarajan/ipfs/file1.js');

    invoke.getfile(req.params.hash).then((str)=>{
        console.log("resu",str)

        res.send({"Result":str});
    })
    .catch((error) => {
        res.send({"Error":"error in fetching the file through the hashcode"});
    })    
});

我正在从后端接收数据;现在我需要将其转换为 angular6

中的图像

您可以使用 btoa function, and then use a Data URL 将字节数组转换为 Base64 编码的字符串以显示图像。不过您需要知道图像的 MIME 类型:

var bytes = [ ... ]; // get from server
var uints = new UInt8Array(bytes);
var base64 = btoa(String.fromCharCode(null, uints));
var url = 'data:image/jpeg;base64,' + base64; // use this in <img src="..."> binding