I was wondering how to view an uploaded picture in jade here are some of my code sample's below
Uploading The Image
app.post('/add', function (req, res, next) {
var person = new Person({
image: './public/images/' + req.files.person.image.name;
});
var tmp_path = req.files.person.image.path;
var target_path = './public/images/' + req.files.person.image.name;
fs.rename(tmp_path, target_path, function (err) {
if (err) {
return next(new Error(err));
}
fs.unlink(tmp_path, function (err) {
if (err) {
console.log(err);
}
person.save(function (err) {
if (err) {
return next(new Error(err.message));
}
res.redirect('/view/' + person._id);
});
});
});
});
Person View Page
app.get('/view/:id', function (req, res, next) {
Person.findById(req.params.id, function (err, person) {
if (err) {
return next(new Error(err));
}
if (!person) {
return next(new Error('Invalid reference to person information'));
}
fs.readFile(person.image, 'binary', function (err, file) {
if (err) {
return next(new Error(err));
} else {
res.render(path + 'view', {
title: title,
person: person,
file: file
});
}
});
});
});
And in my view.jade I use to display the picture
img(src='#{file}')
But its not being displayed,SOMEONE PLEASE HELP ME !!
treat file as a javascript variable inside the parens
img(src=file)
you can try img(src='#{locals.file}')