Here is the HTML code I am working with:
<form >
<input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>
</form>
Inside my controller, I have the following function:
$scope.uploadFile = function() {
var file = $scope.myFile;
var fd = new FormData();
fd.append('file', file);
$http.post("url", fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined},
transformResponse: [function (data) {
return data;
}]
}).then(function (result) {
console.log(result.data);
})
}
The directives I'm using are as follows:
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
I've noticed that I'm getting an undefined value for the file. Is there something wrong in my code?
Upon inspecting the browser's developer tools, it appears that I am passing undefined to the server. Here is a screenshot for reference:https://i.sstatic.net/JhstR.jpg