上传文件nodejs和angular formidable

时间:2017-05-23 07:36:08

标签: javascript angularjs node.js formidable

我使用formidable上传带有angular和nodejs的文件。但我无法向服务器发布任何内容。这是我试过的代码:

服务器

    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()">

          

$scope.formMessage={};
    $scope.pushMessage = function() {

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

1 个答案:

答案 0 :(得分:3)

我以前没用过这个。但ng-fileupload可以帮到你。它简单方便。这是github文档。 https://github.com/danialfarid/ng-file-upload。希望它对你有所帮助