使用 Jade 和 NodeJS 插入图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18039655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Image using Jade and NodeJS
提问by brent
How can I use an object that I've passed through to jade within an image, I'm also using mongodb to hold the data.
我如何使用我在图像中传递给玉的对象,我也在使用 mongodb 来保存数据。
Currently this is my code:
目前这是我的代码:
db.collection('blogposts', function(err, collection) {
if (err) throw err;
collection.find().toArray(function(err, docs) {
if (err) throw err;
res.render('table', { title: 'Blog Posts', tab: "list" , blogposts: docs });
});
});
So I have the nodejs passing through a mongodb collection through to jade, Then within Jade I have:
所以我让nodejs通过mongodb集合传递给jade,然后在Jade中我有:
div.span9
table.table.table-bordered.table-striped.noborder
each row in blogposts
tr
td
div.blogtitle #{row.Title}
br
div.blogheading #{row.Heading}
br
div.namedate #{row.Namedate}
br
div.imagetable
img(src='')
br
div.blogposts #{row.Posts}
br
div.blogtags Tags: #{row.Tags}
And what I'm trying to do is use #{row.Image}within the actual img(src='')as the source.
我想要做的是#{row.Image}在实际中使用img(src='')作为源。
It appears that I must use some other syntax or something to use it within the source as just putting it in doesn't work.
看来我必须使用其他一些语法或其他东西才能在源代码中使用它,因为只是将其放入不起作用。
回答by Peter Lyons
Just do img(src= "http://" + row.Image)
做就是了 img(src= "http://" + row.Image)
Jade will treat the srcattribute value as a javascript expression, evaluate it and render the HTML as you would expect.
Jade 会将src属性值视为 javascript 表达式,对其进行评估并按照您的预期呈现 HTML。
回答by kjonsson
I had a similar issue. My url already had https:// prepended. The solution that worked was:
我有一个类似的问题。我的网址已经有 https:// 前缀。有效的解决方案是:
img(src=row.Image)
回答by Greg S
While if you are sending a link it might work the way has been detailed. If you are sending over the data as a base64 encoded string, then you must prepend the following to your picture data:
如果您发送链接,它可能会按照已详细说明的方式工作。如果您将数据作为 base64 编码字符串发送,则必须在图片数据前添加以下内容:
"data:image/png;base64,"
“数据:图像/png;base64,”
So you would have: img(src= "data:image/png;base64," + row.ImageDataBase64)
所以你会有: img(src= "data:image/png;base64," + row.ImageDataBase64)

