从angular度发送文件到Node Express

我想发送一个文件到我的Node.js应用程序,但似乎应用程序没有收到任何东西。 我不知道该怎么做。 我应该如何检查如果我已经发送的文件,如果我在req收到它?

 <form> <input type = "file" file-model="files" multiple/> <button class="md-primary md-button md-cyan-theme md-ink-ripple" ng-click = "vm.uploadFile()">upload me</button> </form> 

这是我的控制者:

 function uploadFile() { console.log("Load"); var fd = new FormData(); console.log($scope.files) // FileList {0: File, Length: 1} angular.forEach($scope.files, function (file) { fd.append('file', file); }); console.log(fd); // FormData {} (Empty?) $http.post('http://localhost:8090/file-upload'), { headers: {'Content-Type': undefined }, files: fd }).success(function (d) { console.log(d); }); } 

这是我的指示:

 .directive('fileMode', ['$parse', function ($parse) { return { restrict: 'A', link: function (scope, element, attrs) { element.bind('change', function () { $parse(attrs.fileModel).assign(scope, elemtn[0].files); scope.$apply(); }); } }; }]) 

这是我的快速应用程序:

 app.post('/file-upload', function (req, res, next) { console.log("Sent!"); var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, './uploads'); }, filename: function (req, file, cb) { cb(null, file.fieldname); } }); var upload = multer({ storage : Storage }).array('userPhoto', 2); upload(req, res, function (err) { console.log(req.body.data.files); if (err) return res.end("Error uploading file."); res.end("File is uploaded."); }) }) 

感谢您的帮助。

       

网上收集的解决方案 "从angular度发送文件到Node Express"

networkingforms和多pathconfiguration中的字段名称不一样:

 var upload = multer({ storage : storage }).array('file',2); 

希望这会工作!

 var express = require('express'), app = express(), bodyParser = require('body-parser'), multer = require('multer'); app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "http://localhost"); res.header( "Access-Control-Allow-Origin", "Origin, X-Requested-With, Content-Type, Accept" ); next(); }); app.use(express.static('../client')); app.use(bodyParser.json()); var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './uploads/'); }, filename: function (req, file, cb) { var split = file.originalname.split('.'); cb(null, file.fieldname + "-" + Date.now() + "." + split[split.length - 1]); } }); var upload = multer({ storage : storage }).single('file'); app.post('/upload', function (req, res) { upload(req, res, function (err) { if (err) res.json({ error_code: 1, err_desc: err }); res.json({error_code: 0, err_desc: null }); }) }) app.listen('3000', function () { console.log("Running on 3000"); })