简体   繁体   中英

File upload using Angular and Node

I'm trying to achieve the following:

  • Client uploads a CV as part of a form
  • AngularJS sends all the form data (including CV) to the Node server
  • Node stores the CV on the server

I just can't seem to get this working. I tried using multer to no avail.

So far I'd have the following:

HTML

<form name="register" enctype="multipart/form-data" method="post" ng-submit="registerSubmit(user)">
  <input type="email" placeholder="Email" ng-model="user.email" />
  <input type="file" id="cv" name="cv" ng-model="user.cv" />
  <button type="submit"></button>
</form>

Angular

app.controller("MainController", function($scope, $http){

  $scope.registerSubmit = function(user){
    $http.post("/api/register", user).success(function(data){
      console.log(data);
    });
  });

});

Node (configured with Express and other modules)

app.use("/api", router);
router.post("/api/register", function(req, res){
  //file not coming through and not sure what to do with it here anyway!
});

You must make your request encoded as "multipart/form-data". Check this simmilar question: file upload with $http

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM