I am using MangoDB along with GridFS to store images. I've got this route to retrieve an image from the DB :
app.get("/images/profile/:uId", function (req, res) {
let uId = req.params.uId;
console.log(uId)
gfs.files.findOne(
{
"metadata.owner": uId,
"metadata.type": 'profile'
}
, function (err, file) {
if (err || !file) {
return res.status(404).send({ error: 'Image not found' });
}
var readstream = gfs.createReadStream({ filename: file.filename });
readstream.on("error", function (err) {
res.send("Image not found");
});
readstream.pipe(res);
});
});
This returns something like that :
?PNG
IHDR???]?bKGD???? pHYs
?
?B(?xtIME? -u??~IDATx??i|TU????JDV?fH?0? :-
H_??M??03`???
(??-?q{U[?m?A??AQ?VZ??bP??S?@K@??CB??|??T?[?=????"U??[??{?s?9?
?+)@e????{?9???,??T.S??xL?x&@?0TSFp7???t????A!_????D?h?
z????od?G???YzV??e???|?h???@P?,?{???????Z?lvc?N???
n??(?r?.......
It seems like I get the png correctly. How do I display it in an img tag then ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…