I have tried to retrieve images I uploaded and saved to mongodb but I am not able to retrieve back the image directly from db. There seems to be a lot of information about uploading files but not a lot of information about retrieving the uploaded images from the db.
I would really appreciate your help. Thanks in advance.
This is part of what is saved in mongodb:
{
"_id": {
"$oid": "58e9aba1cc06152fd0d3bc56"
},
"img": {
"data": "<Binary Data>",
"contentType": "image/png"
}
}
Schema:
var mongoose = require('mongoose');
var ImageSchema = new mongoose.Schema({
img: { data: Buffer, contentType: String }
});
module.exports = mongoose.model('Image', ImageSchema);
router.js
router.post('/blog/article/addPost', upload.single('image'), function(req, res, err) {
var image = new Image ({
img: {
data: req.file.filename,
contentType: 'image/png'
}
});
image.save(function(err, image) {
//other code
});
});
Part of ejs to upload file
<form action="/blog/article/addPost" method="post" enctype="multipart/form-data">
<div class="form-group">
<label="name">Add a picture for your article</label>
<input id="upload-input" type="file" class="form-control" name="image"/>
</div>
</form>
ejs to retrieve file:
<div class="col-md-4">
<form method="GET" action="/blog/article/<%= image.id %>">
<img src="<%= image.img %>" style="width:304px;height:228px;">
</form>
</div>
via CODI
No comments:
Post a Comment