[英]node uploading file $http.post from angularjs has undefined req.body
我正在使用angularjs应用程序构建文件上传功能,该功能会将文件上传到我的节点api,该文件将ftp到cdn服务器。 现在,我只能获取hte文件。 我尝试使用multer,但不确定如何防止保存重定向到ftp。
无论如何,这是我的代码,没有覆盖
<input type="file" multiple file-model="fileRepo"/>
myApp.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.saveFile = function(){
var fd=new FormData();
angular.forEach($scope.fileRepo,function(file){
fd.append('file',file);
});
$scope.newFile.files = fd;
FileService.uploadFile($scope.newFile)
.....
///文件服务///
uploadFile: function(file){
var deferred = $q.defer();
var uploadUrl = '/api/file/ftp/new';
var requestFileUpload = {
method: 'POST',
url: uploadUrl,
data: file.files
}
var requestFileUploadConfig = {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
$http.post(uploadUrl, file.files, requestFileUploadConfig)
.then(function(){
})
///节点部分///
router.post('/ftp/new', function(req, res) {
console.log('file is ' + JSON.stringify(req.body));
});
您将必须使用HTML解析器,仅通过读取请求就无法捕获文件。
我建议使用busboy和connect-busboy,然后您将能够读取文件,这是一个小例子:
req.pipe(req.busboy);
req.busboy.on('file',function(fieldname, file, filename, encoding, contentType){
// get data
file.on('data',function(data){
}).on('end', function(){
});
});
req.busboy.on('field',function(fieldname, val){
req.body[fieldname] = val;
});
req.busboy.on('finish', function() {
// save file here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.