Tuesday, 23 May 2017

upload file nodejs and angular formidable

I'm use formiable to upload file with angular and nodejs . But I cant post anything to server . Here is code I tried

server

    var form = new formidable.IncomingForm();

    form.uploadDir = path.join(__dirname, '../public/uploads');
 form.on('file', function(field, file) {
    fs.rename(file.path,path.join(form.uploadDir,file.name),function(err) {
        if(err)
            console.log(err);
        console.log('Success')
    });


});
   // log any errors that occur
    form.on('error', function(err) {
        console.log('An error has occured: \n' + err);
    });


    // parse the incoming request containing the form data
    form.parse(req, function(err, fields, files) {
    });
})

Html

<form enctype="multipart/form-data" class="form-horizontal" role="form" id = "form_email" ng-submit="pushMessage()">
<input type="file" name="filesuploads[]" > 
</form>

Angular

$scope.pushMessage = function() {

  $http.post('/customers/message').then(function(response) {
    console.log(response);
  });
};

Please help me . Any help would be upvoted



via Nguyễn Bảo Dũng

No comments:

Post a Comment