【发布时间】:2017-02-10 10:32:09
【问题描述】:
我正在尝试使用 Mongoose、Express 和 Angular 上传和存储图片。我在这里选择了下一个解决方案:
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('change', function(){
$parse(attrs.fileModel).assign(scope,element[0].files)
scope.$apply();
});
}
};
}])
以及控制器中的下一个功能:
$scope.uploadFile=function(){
var fd = new FormData();
angular.forEach($scope.files,function(file){
fd.append('file',file);
});
$http.post('http://' + host + ':3000/users/' + $scope.selectedTask._id,fd,
{
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).success(function(d){
console.log('yes');
})
}
还有html:
<input type = "file" file-model="files" multiple/>
<button ng-click = "uploadFile()">upload me</button>
<li ng-repeat="file in files">{{file.name}}</li>
但由于某种原因,我在端点中得到的只是一个空请求对象。我正在使用 express.js 中的以下代码检查它:
user.route('/users/:id')
.post(function (req, res, next) {
console.log(req.body);
})
我认为问题在于我不知道如何存储大于 16MB 的内容。
【问题讨论】:
-
你为什么不使用 multer 一个节点包来帮助你存储任何类型的文件。如果你愿意,我可以告诉你如何使用它。
-
@sacDahal 这是我第一次听说,我是 node 新手。如果你告诉我如何上传图片,我会很高兴。但我想将它存储在 mongodb 中,因为我在那里有一个用户群,我想将图片与其他用户的数据一起存储。
标签: angularjs node.js express mongoose