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

编程入门 行业动态 更新时间:2024-10-25 03:19:58
本文介绍了将字节数组转换为 angular6 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已从服务器以字节数组形式发送图像文件.现在我必须将其转换为 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 00:28:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字节   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!