javascript - Cannot send a file through FormData object in http request. Formidable cannot find any file in server end -
i want upload file angularjs in client side , nodejs , express in server side. in angular, i'm using lf-ng-md-file-input directive upload file. in server side, i'm using package formidable parse multipart form data. here html code:
<lf-ng-md-file-input lf-submit-label="upload" lf-files="files" submit lf-on-submit-click="onsubmitclick" ng-change="onsubmitfileschange()" progress> </lf-ng-md-file-input>
and angular code submit click:
$scope.onsubmitclick = function(files) { console.log(files[0]) //this shows file object correctly console.log($scope.file) var formdata = new formdata() angular.foreach(files,function(obj){ if(!obj.isremote){ console.log(obj) formdata.append('files', obj.lffile); } }); formdata.append('source', $scope.file.source); formdata.append('batch', $scope.file.batch); (var pair of formdata.entries()) { console.log(pair[0]+ ', ' + pair[1]); } $http.post(config.serverurl + "/upload", formdata, { transformrequest: angular.identity, headers: {'content-type': undefined} }).then(function(result){ // sometingh console.log(result) },function(err){ // sometingh }); }
this server side code in node js:
var formidable = require('formidable') app.use(express.static(__dirname + '/public')) app.post('/upload',function(req,res){ var form = new formidable.incomingform(); form.uploaddir = __dirname +'/public/uploads'; form.keepextensions = true; console.log(form) console.log(req.body) //file upload path form.parse(req, function(err, fields, files) { //you can fields here console.log("parsing") console.log(fields) console.log(files) }); form.on ('filebegin', function(name, file){ console.log("file begin") file.path = form.uploaddir + "/" + file.name; //modify file path }); form.on('progress', function(err) { console.log("on progress") }); form.on('field', function(err) { console.log("fields found") }); form.on('file', function(err) { console.log("file found") }); form.on('error', function(err) { console.log("error occured") res.sendstatus(400); }); form.on('aborted', function(err) { console.log("aborted") }); form.on ('end', function(){ console.log("in end") res.sendstatus(200); //when finish process }); });
but problem is, in server side, req.body showing other fields there nothing found in form. shows aborted due timeout. form.parse not working @ all.
please suggest how can find file in form object @ server side
Comments
Post a Comment