简体   繁体   English

是否可以将 mongoDB 中的二进制图像数据存储渲染到车把模板?

[英]Is it possible to render binary images data store in mongoDB to handlebars template?

I´m looking for some help.我正在寻求帮助。 I´m creating a gallery application using nodejs, mongoDB and handlebars template to show the gallery.我正在使用 nodejs、mongoDB 和车把模板创建一个画廊应用程序来显示画廊。 Everything goes fine till I find all images in my mongoDB (stored as Binary Data) and try to render in my handlebar template.一切都很顺利,直到我在 mongoDB(存储为二进制数据)中找到所有图像并尝试在我的车把模板中渲染。 I'm trying this:我正在尝试这个:

routes.js路由.js

router.get('/gallery', async (req, res) =>{
    const pics = await Img.find();
    console.log(pics);    // No problems showing images as binary data and all fields in the Schema
    res.render('images/gallery.hbs', {pics}); 
});

template.hbs模板.hbs

    {{#each pics}}
        <div class="row"> 
           <img src='????'> 
            </div> 
    {{/each}}

where????在哪里???? is my big problem, I don't know what's the code to put there or if I'm using the right way.是我的大问题,我不知道放在那里的代码是什么,或者我是否使用了正确的方法。 Pls help.请帮忙。 I'm newer on this我对此较新

if you are storing binary you can be added to the src of course, but you should consider the metadata associated with the binary like this:如果您要存储二进制文件,当然可以将其添加到src中,但您应该考虑与二进制文件关联的元数据,如下所示:

<img src="data:image/png;base64,...">

Note: This is a bad practice by the way, and you should never save the images on the DB whatever the case, you can use external service like aws s3 and upload your images there and store their Uri instead, they're a lot of reasons for this the biggest one it gonna cost you a lot of money -_-注意:顺便说一句,这是一种不好的做法,无论如何您都不应该将图像保存在数据库中,您可以使用 aws s3 之类的外部服务并将图像上传到那里并存储它们的 Uri,它们很多这是最大的原因,它会花费你很多钱-_-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM