admin管理员组文章数量:1346198
I have the following code on an express server, to read an image and send it to the client via api
router.get("/file",async (req,res)=>{
const objImg = {img:null}
const result = await SELECT("...");
if(result.length>0){
var bitMap= fs.readFileSync(`./src/logosClient/${result.nombImg}`)
objImg.img=new Buffer.from(bitMap,"base64")
}
res.json(objImg)
})
I have the following code on an express server, to read an image and send it to the client via api
router.get("/file",async (req,res)=>{
const objImg = {img:null}
const result = await SELECT("...");
if(result.length>0){
var bitMap= fs.readFileSync(`./src/logosClient/${result.nombImg}`)
objImg.img=new Buffer.from(bitMap,"base64")
}
res.json(objImg)
})
The api data arrives as follows
data:{type: "Buffer", data: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 192, 0, 0, 0, 46, 8, 6......]}
In my ReactJs client, I have the following code to receive it
import React, {useEffect, useState} from 'react';
import muestra from '../../../../resources/images/muestra.png'
const Index = () => {
const [previewImg, setPreviewImg] = useState(null)
useEffect(()=>{
RunApi("/generales/file","GET",null,null).then(result=>{
if(result.img){
setPreviewImg("data:image/png;base64," + result.img.data)
}
}).catch(error=>{
addToast("error","Error",error)
})
},[])
return(
<img src={previewImg?previewImg:muestra} className="align-self-center mr-3" alt="..."/>
)
}
I can't see the image but it doesn't generate any error message either
Share Improve this question edited Mar 27, 2020 at 19:30 ulentini 2,4121 gold badge15 silver badges25 bronze badges asked Mar 27, 2020 at 19:29 Jose FdoJose Fdo 1391 gold badge2 silver badges9 bronze badges 2- Did you check what data do you receive client-side? Do you actually receive the base64 string in the response? – ulentini Commented Mar 27, 2020 at 19:33
- The api data arrives as follows data:{type: "Buffer", data: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 192, 0, 0, 0, 46, 8, 6......]} – Jose Fdo Commented Mar 27, 2020 at 19:36
1 Answer
Reset to default 9Try to convert your image to base64 string like
objImg.img = new Buffer.from(bitMap).toString("base64")
And then set your state like
setPreviewImg("data:image/png;base64," + result.img)
本文标签: javascriptConvert a Buffer Array into an imageStack Overflow
版权声明:本文标题:javascript - Convert a Buffer Array into an image - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743823084a2545144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论