【问题标题】:How to send buffer image data from node js to ejs template如何将缓冲区图像数据从节点 js 发送到 ejs 模板
【发布时间】:2020-09-16 00:57:08
【问题描述】:

我正在练习使用 node js 从数据库中上传和获取图像。上传部分进行得很顺利,但是获取部分出现了一些问题,获取图片的代码是app.get('/images', async (req, res) => { const img = await image(image is the model with type of buffer).find({}); res.render('image', { img }) }。 ejs 模板部分是<% img.forEach(i => { <img src="data:img/png;base64,<%= i.avatar(avatar is property on the image model) %>"> }。 有人能告诉我怎么做吗??

【问题讨论】:

    标签: node.js ejs


    【解决方案1】:

    您可能应该将avatar 缓冲区映射到base64 编码的字符串并将其传递给ejs-模板:

    app.get('/images', async (req, res) => {
        const img = await image().find({});
        img.avatar = Buffer.from(img.avatar).toString('base64');
        res.render('image', {img});
    });
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2021-01-24
      • 2021-08-17
      • 2020-07-06
      相关资源
      最近更新 更多