Express发送base-64编码的png-image

时间:2015-12-04 23:07:10

标签: node.js express

在我的node.js应用中,我试图回复一个图片。

此图像在postgresql之前保存为文本。

文字看起来像这样:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAE

但是当我尝试将其作为图像返回时:

    res.type('image/png');
    res.send(image_string); 

或二元:

     res.send(image_string,'binary'); 

它显示一个空的image-element: enter image description here

我错了什么?谢谢

1 个答案:

答案 0 :(得分:6)

我通过使用缓冲区来解决它:

const im = image_string.split(",")[1];

const img = Buffer.from(im, 'base64');

res.writeHead(200, {
   'Content-Type': 'image/png',
   'Content-Length': img.length
});

res.end(img); 
相关问题