【问题标题】:Express send base-64 encoded png-image快递发送 base-64 编码的 png-image
【发布时间】:2015-12-04 23:07:10
【问题描述】:

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

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

文本看起来像这样:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAE

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

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

或二进制:

     res.send(image_string,'binary'); 

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

我做错了什么?谢谢

【问题讨论】:

    标签: node.js express


    【解决方案1】:

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

    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); 
    

    【讨论】:

    • 很好的答案,@John Smith。只是想提醒人们删除 data:image 部分,如下所示:``` const buffer = new Buffer.from(base64Img.replace(/^data:image\/\w+;base64,/, ""), ' base64'); ```
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2013-09-21
    • 2014-11-30
    • 1970-01-01
    • 2011-10-11
    • 2020-01-31
    相关资源
    最近更新 更多