[英]How do I get ng-file-upload to work with Node?
I am using Angular with Node, Express, and Multer and ng-file-upload. 我正在将Angular与Node,Express和Multer和ng-file-upload一起使用。 For some reason I am getting 400 (bad request) http request error.
由于某种原因,我收到400(错误请求)http请求错误。 I tried different things but this is all is coming back.
我尝试了不同的方法,但是一切都回来了。 Here is the code sample.
这是代码示例。
HTML 的HTML
<form ng-controller="adminController" name="form">
Single Image with validations Select 带有验证的单个图像选择
submit 提交
Angular app.js Angular app.js
$scope.submit = function() {
$log.info($scope.file);
if ( $scope.file) {
$scope.upload($scope.file);
}
};
// upload on file select or drop
$scope.upload = function (file) {
Upload.upload({
url: '/api/admin/photos',
headers : {
'Content-Type': 'multipart/form-data'
},
data: {file: file}
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
};
Node js 节点js
//upload photos
apiRoutes.post('/admin/photos',upload.single(),function(req,res){
console.log(req);
res.json({name: 'this works'});
});
upload.single()
should instead be upload.single('file')
since that is the property name you're using for your upload. upload.single()
应该改为upload.single('file')
因为这是您用于上载的属性名称。 Also, explicitly setting: 另外,显式设置:
headers : {
'Content-Type': 'multipart/form-data'
}
is unnecessary. 是不必要的。
If that still doesn't work, you should temporarily remove upload.single('file')
and ensure that your route handler is being reached to begin with (and some prior middleware/route handler hasn't intercepted it). 如果那仍然不起作用,则应暂时删除
upload.single('file')
并确保upload.single('file')
开始到达路由处理程序(并且某些先前的中间件/路由处理程序尚未拦截它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.