将字节数组转换为 angular6 中的图像 [英] convert byte array to image in angular6

查看:23
本文介绍了将字节数组转换为 angular6 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have sent the image file in a byte array from the server. Now I have to convert that into a jpeg file and display it in a webpage.

代码:

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 中的图像

I am receiving the data from the backend; now I need to convert this into an image in angular6

推荐答案

您可以使用 btoa 函数,然后使用 数据 URL 以显示图像.不过,您需要知道图像的 MIME 类型:

You can convert a byte array to a Base64-encoded string using the btoa function, and then use a Data URL to display the image. You will need to known the image's MIME type though:

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

这篇关于将字节数组转换为 angular6 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆