来自 angularjs 的节点上传文件 $http.post 未定义 req.body
node uploading file $http.post from angularjs has undefined req.body
我正在使用我的 angularjs 应用程序构建文件上传功能,该应用程序会将文件上传到我的节点 api,然后 ftp 到 cdn 服务器。现在我只能获取 hte 文件。我尝试使用 multer,但我不确定如何防止保存重定向到 ftp。
无论如何,这是我没有multer的代码
<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 and 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
});
我正在使用我的 angularjs 应用程序构建文件上传功能,该应用程序会将文件上传到我的节点 api,然后 ftp 到 cdn 服务器。现在我只能获取 hte 文件。我尝试使用 multer,但我不确定如何防止保存重定向到 ftp。
无论如何,这是我没有multer的代码
<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 and 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
});