I am trying to upload form with image file with angular $http function and using multer at background to receive. I know how to submit the form directly(without angular), I can successfully upload it via:
<form method="post" enctype="multipart/form-data">
<input type="file" name="avatar" /><br/>
<input type="submit" value="submit"/><br/>
</form>
In this way, the image will be at req.file. However, when I try to use angular http to upload it:
<form name="myForm" ng-model="formData" enctype="multipart/form-data">
<input type="file" file-model="avatar" name="avatar" required/><br/>
<label class="item item-input">
<button class="button button-block button-positive" ng-click="submit()" class="btn btn-primary btn-block">submit</button>
</label>
<label class="item item-input">
<button class="button button-block button-dark" ng-click="home()" >back</button>
</label>
</form>
js:
$scope.submit = function() {
var fd = new FormData();
fd.append('avatar', $scope.avatar);
$http({
url:'/avatar',
method: 'POST',
data: fd,
headers: {
'Content-Type': 'undefined'
},
transformRequest: angular.identity
}).success(function(data,status){
if(status == 200) {
window.location = '/home';
}
}).error(function(status, data){
window.location = '/avatar';
})
}
the req.file field become undefined. So how to upload my image via angular $http?