2016-12-05 48 views
-1

我在上传文件时遇到问题。我有一个表单将数据发送到我的路由器文件中的控制器。当数据发布时,请求文件未发现。我试图做的是将图像发送给我的公众/图像,然后将图像名称放入我的候选人中。尝试发布表单信息时,请求文件未定义

这里是我的形式

<div class="page-wrap text-center"> 
<div ng-controller = "candidatesController"> 
    <div class = "row"> 
     <div class=" col-md-6 col-md-offset-3"> 
      <form class = "content-margin" ng-submit="addCandidate()" style="margin-top:30px" enctype = "multipart/form-data"> 
       <h3 class = "formheading"> Add a new Candidate</h3> 

       <div class = "form-group" align = "center"> 
        <input type = "string" class = "form-control" placeholder = "candidate name" ng-model = "formData.name" ng-style = "{'width':150 + 'px'}"></input> 
       </div> 
       <div class = "form-group" align = "center"> 
        <input type = "string" class = "form-control" placeholder = "candidate role" ng-model = "formData.role" ng-style = "{'width':150 + 'px'}"></input> 
       </div> 
       <div class = "form-group" align = "center"> 
        <input type = "string" class = "form-control" placeholder = "candidate email" ng-model = "formData.email" ng-style = "{'width':150 + 'px'}"></input> 
       </div> 
       <input method = "post" type="file" name="userPhoto" nd-model = "file"/> 

       <button type="submit" class="btn btn-primary">Add Candidate</button> 
      </form> 
     </div> 
    </div> 
</div> 
</div> 

,这里是我的控制器

router.addPerson = function(req, res) { 

    console.warn(req.files); 



    var candidate = new CandidateModel(); 

    candidate.name = req.body.name; 
    console.warn(req.body.name); 
    candidate.role = req.body.role; 
    console.warn(req.body.role); 
    candidate.email = req.body.email; 
    console.warn(req.body.email); 
    console.warn(candidateImage); 


    // Save the donation and check for errors 
    candidate.save(function(err) { 
     if (err) 
      res.send(err); 

     res.json({ message: 'Donation Added!', data: candidate }); 
    }); 

} 

我没有文件上传代码没有完成,因为我不能甚至可以被发送过来的文件。这是我的控制器中的功能。

$scope.updateCandidate = function(){ 

    $scope.formData.id = candidateId.getID(); 

    $http.post('/candidates/'+ candidateId.getID(), $scope.formData) 
     .success(function(data){ 
      console.log(data); 
      $scope.candidates = data; 
      $location.path('/candidates'); 
     }) 
     .error(function(data){ 
      console.log('Error:' + data); 
     }); 
} 

任何帮助将是巨大的

+0

发送请求? Multer,公务员?也许是自定义的? – peteb

+0

我正在使用Mulder – ToljinSaturn

回答

0

看你的角度代码,你不设置Content-Typemultipart/form-data。默认情况下使用$http.post()application/json

return $http({ 
    method: 'POST', 
    url: '/candidates/' + candidateId.getId(), 
    data: $scope.formData, 
    headers: { 
    'Content-Type': 'multipart/formadata' 
    } 
}) 
    .then((data) => { 
    // handle data 
    }) 
    .catch((err) => { 
    // handle error 
    }); 
您正在使用什么文件上传中间件