Node.js MongoDB Gridfs节点显示图像
我使用GridFS在MongoDB中存储angular应用程序中的图像。从数据库获取图像时出现问题。我只能在iexplorer和postman中显示图像。在chrome和firefox中,它看起来像是损坏的图像。 有没有办法直接从数据库以角度显示图像?我在用快车。然后尝试像这样插入图像:Node.js MongoDB Gridfs节点显示图像,node.js,mongodb,Node.js,Mongodb,我使用GridFS在MongoDB中存储angular应用程序中的图像。从数据库获取图像时出现问题。我只能在iexplorer和postman中显示图像。在chrome和firefox中,它看起来像是损坏的图像。 有没有办法直接从数据库以角度显示图像?我在用快车。然后尝试像这样插入图像: 我的路由器 router.get('/upload/:objectId',function(req, res){ var options = { _id : req.params.o
我的路由器
router.get('/upload/:objectId',function(req, res){
var options = {
_id : req.params.objectId
};
gfs.exist(options, function(err, exists) {
if(!exists) {
res.status(404);
res.end();
} else {
res.set('Content-Type', 'image/jpeg');
var readstream = gfs.createReadStream(options);
readstream.pipe(res);
}
});
});
解决了,
router.get('/upload/:image',function(req, res){
try{
var readstream = gfs.createReadStream({ filename: req.params.image});
res.set('Content-Type', 'image/jpeg');
readstream.pipe(res);
}
catch (err) {
log.error(err);
return next(errors.create(404, "File not found."));
}
});
html:
<img src="/usr-rest/users/upload/here_goes_id">