使用multer,ng-file-upload,angular和node将dynamic表单数据绑定到每个file upload多个文件

我在客户端使用angular和ng-file-upload。 我在服务器端使用节点,expression式和multer

当用户select多个文件时,我会显示文件的预览以及绑定到模型的textarea。 文本区域的这个目的是为用户提供给每个上传的文件一个笔记/描述的能力。

当我调用ng-file-upload的上传函数并传入文件的模型时,我看到每个文件特有的dynamic表单数据存在于正在上传的数组中。

在服务器端我使用的是Muller。 一切似乎function正常。 我可以上传多个文件,但是dynamic表单数据在multer内置的req.files对象上不可用,我不知道在哪里可以访问dynamic数据。

我search了请求对象,找不到我正在查找的数据。

如何将dynamic数据分别添加到每个文件并将其传递给服务器并访问?

客户

$scope.uploadFiles = function(files) { console.log('files', files); //I see that this argument contains the user input as property 'Desc' if (files && files.length) { Upload.upload({ url: 'api/filesinorg/upload/'+ $rootScope.currentUser.primary_org_id, arrayKey: '', // default is '[i]' data: { files: files, fileData: { Organization_id: $rootScope.currentUser.primary_org_id, uploadBy_id: $rootScope.currentUser._id } } }) .then(function (result) { $timeout(function () { $scope.filesSelected = []; $scope.uploadedFiles.push(result.data.files); }); }, function (result) { if (result.status > 0) { $scope.errorMsg = result.status + ': ' + result.data; } }, function (evt) { $scope.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total)); }); } }; 

服务器

 var multer = require('multer'); var uploadPath = ''; var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/'+ req.params.orgId + '/') }, filename: function (req, file, cb) { cb(null, file.originalname); } }); var upload = multer({ storage: storage }); var uploadRoot = 'uploads/'; function checkUploadPath(req, res, next) { var orgId = req.params.orgId; uploadPath =uploadRoot + orgId + '/'; fs.stat(uploadPath, function(err, stats) { if(err) { console.log('Error', err); fs.mkdir(uploadPath, function(err) { if(err) { console.log('Error in folder creation'); next(); } next(); }); } else { next(); } }); } var type = upload.array('files'); router.use('/filesinorg/upload/:orgId', checkUploadPath, type); router.route('/filesinorg/upload/:orgId') .post(function(req, res) { var orgId = req.params.orgId; var filesUploaded = req.files; var fileData = req.body.fileData; var isError = false; var filesSaved = []; filesUploaded.forEach(function(file) { console.log('file', file); console.log('file.Desc', file.Desc); var newFile = new File(); newFile.fileName = file.originalname; newFile.serverPath = file.path; newFile.shortDesc = file.Desc.slice(0,20); newFile.Desc = file.Desc; newFile.upload_dt = Date.now(); newFile.uploadBy_id = fileData.uploadBy_id; newFile.Organization_id = fileData.Organization_id; newFile.type = file.mimetype; file.save(function(err, savedFile) { if (err) { return isError = true; } filesSaved.push(newFile); console.log('File Saved success fully'); }); }); if(isError) { res.status(500).send({ success: false, msg: 'Error Saving File', file: null }) } else { res.status(200).send({ success: true, msg: 'Saved Contact', files: filesSaved }); } }); 

       

网上收集的解决方案 "使用multer,ng-file-upload,angular和node将dynamic表单数据绑定到每个file upload多个文件"