使用ng-file-upload将照片从前端发送到服务器端这是我的代码:
查看:
<form name="photoForm" ng-submit="uploadPicture(files)">
<input type="text" class="form-control" value="Add Caption" ng-model="caption">
<input type="button" name="files" value="Upload picture" ngf-select ng-model="files" ngf-multiple="true" ngf-pattern="'image/*'">
<input type="submit" class="btn btn-default submitButton" ng-disabled="!photoForm.$valid">
<ul>
<li ng-repeat="f in files" style="font:smaller">
{{f.name}}{{f.$errorParam}}
</li>
</ul> </form
控制器:
// Upload Image
$scope.uploadPicture = function(files){
if (files && files.length) {
$scope.uploadFiles(files);
}
};
$scope.uploadFiles = function(files){
Upload.upload({
url: '/api/posts/photo',
method: 'POST',
file: files
}).then(function (response) {
$timeout(function () {
$scope.result = response.data;
});
}, function (response) {
if (response.status > 0) {
$scope.errorMsg = response.status + ': ' + response.data;
}
});
};
路线:
var multipart = require('connect-multiparty'),
multipartMiddleware = multipart();
app.route('/api/posts/photo')
.post(multipartMiddleware, photos.create);
服务器:
exports.create = function(req, res){
console.log(req.body, req.files);
};
什么时候我的console.log(req.files),没有multipartMiddleware我能够看到我上传的文件,但是一旦我添加了multipartMiddleware,错误信息就会显示出来。