我已从服务器发送字节数组中的图像文件。现在我必须将其转换为jpeg
文件并将其显示在网页中。
Code:
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 中的图像
您可以使用以下命令将字节数组转换为 Base64 编码的字符串btoa函数,然后使用Data URL显示图像。不过,您需要知道图像 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)