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

时间:2023-01-20 18:30:44

In my node.js app I`m trying to respond with an image.

在我的node.js应用程序中,我试图用图像进行响应。

This image was saved before postgresql as text.

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

The text looks just like this:

文字看起来像这样:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAE

But when I try to return it as an image:

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

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

Or binary:

或二进制:

     res.send(image_string,'binary'); 

It shows a empty image-element: Express发送base-64编码的png-image

它显示了一个空的图像元素:

What do I wrong?Thanks

我错了什么?谢谢

1 个解决方案

#1


3  

I solved it by using a buffer:

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

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

var img = new Buffer(im, 'base64');

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

res.end(img); 

#1


3  

I solved it by using a buffer:

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

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

var img = new Buffer(im, 'base64');

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

res.end(img);