我上传的图片使用multer,node.js,mongodb。如何以角度显示图像(图像在服务器端上传文件夹和angularjs在不同的服务器上工作)?
我上传了上传文件夹中的图片,并将路径存储在MongoDB中。
这是我的文件夹结构
服务器是基于文件的ID我retriving文件运行
http://localhost:3000/images/590051dcc90cf702e452b9c1
// 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":"[email protected]","originalname":"login.jpg","__v":0}
我正在向angular和andriod应用程序发送上述响应。
现在我想以角度显示此图像。
角服务器工作在不同势服务器节点服务器在不同的服务器
我把这样的
</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文件位于服务器工作边
现在显示的图像,但我没有获取“名称”:“asdf”和“email”:“[email protected]” –
只是将图像base64字符串分配给流派对象的路径属性。我已经更新了我的答案。如果解决了您的问题,请接受答案。 – Kalyan
@itsme:请检查我的更新答案。 – Kalyan