2016-12-05 53 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 
    }); 
您正在使用什麼文件上傳中間件