【发布时间】:2017-05-05 03:51:27
【问题描述】:
我使用 multer,node.js,mongodb 上传图片。
我在上传文件夹中上传了图片,并将路径存储在 MongoDB 中。
这是我的文件夹结构
服务器正在运行
http://localhost:3000/images/590051dcc90cf702e452b9c1
基于文档 ID 我正在检索文档
// To get the single image/File using id from the MongoDB
app.get('/images/:id', function(req, res) {
//calling the function from index.js class using routes object..
routes.getImageById(req.params.id, function(err, genres) {
if (err) {
throw err;
}
//res.download(genres.path);
res.send(genres)
});
});
我收到了这样的回复
{"_id":"590051dcc90cf702e452b9c1","path":"uploads\login.jpg","name":"asdf","email":"asdf@gmail.com","originalname":"login.jpg","__v":0}
我正在向 Angular 和 Android 应用程序发送上述响应。
现在我想以角度显示此图像。
角度服务器在不同的服务器节点服务器在不同的服务器工作
我是这样写的
</head><body>
<body ng-controller="RegisterCtrl" ng-app="myApp">
<div ng-init="signin()">
<img ng-src="{{response.path}}"/>
{{response}}
</div>
</body>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js'></script>
<script>var myApp = angular.module('myApp', []);
myApp.controller('RegisterCtrl', function ($scope,$http) {
$scope.signin = function()
{
$http.get('http://localhost:3000/images/590051dcc90cf702e452b9c1').success(function(data, response)
{
$scope.response = data;
console.log(data);
});
}
});
</script>
</body></html>
我遇到了错误
file:///C:/Users/mohan/Desktop/uploads/login.jpg net::ERR_FILE_NOT_FOUND
bcoz 文件位于服务器端
【问题讨论】:
标签: javascript angularjs node.js mongodb multer