First of all
<input accept="image/*" name="file" ng-value="fileToUpload"_x000D_
value="{{fileToUpload}}" file-model="fileToUpload"_x000D_
set-file-data="fileToUpload = value;" _x000D_
type="file" id="my_file" />
_x000D_
1.2 create own directive,
.directive("fileModel",function() {_x000D_
return {_x000D_
restrict: 'EA',_x000D_
scope: {_x000D_
setFileData: "&"_x000D_
},_x000D_
link: function(scope, ele, attrs) {_x000D_
ele.on('change', function() {_x000D_
scope.$apply(function() {_x000D_
var val = ele[0].files[0];_x000D_
scope.setFileData({ value: val });_x000D_
});_x000D_
});_x000D_
}_x000D_
}_x000D_
})
_x000D_
$httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript'; $httpProvider.defaults.headers.post['Content-Type'] = 'multipart/form-data; charset=utf-8';
Then create separate function in controller to handle form submit call. like for e.g below code:
In service function handle "responseType" param purposely so that server should not throw "byteerror".
transformRequest, to modify request format with attached identity.
withCredentials : false, for HTTP authentication information.
in controller:_x000D_
_x000D_
// code this accordingly, so that your file object _x000D_
// will be picked up in service call below._x000D_
fileUpload.uploadFileToUrl(file); _x000D_
_x000D_
_x000D_
in service:_x000D_
_x000D_
.service('fileUpload', ['$http', 'ajaxService',_x000D_
function($http, ajaxService) {_x000D_
_x000D_
this.uploadFileToUrl = function(data) {_x000D_
var data = {}; //file object _x000D_
_x000D_
var fd = new FormData();_x000D_
fd.append('file', data.file);_x000D_
_x000D_
$http.post("endpoint server path to whom sending file", fd, {_x000D_
withCredentials: false,_x000D_
headers: {_x000D_
'Content-Type': undefined_x000D_
},_x000D_
transformRequest: angular.identity,_x000D_
params: {_x000D_
fd_x000D_
},_x000D_
responseType: "arraybuffer"_x000D_
})_x000D_
.then(function(response) {_x000D_
var data = response.data;_x000D_
var status = response.status;_x000D_
console.log(data);_x000D_
_x000D_
if (status == 200 || status == 202) //do whatever in success_x000D_
else // handle error in else if needed _x000D_
})_x000D_
.catch(function(error) {_x000D_
console.log(error.status);_x000D_
_x000D_
// handle else calls_x000D_
});_x000D_
}_x000D_
}_x000D_
}])
_x000D_
<script src="//unpkg.com/angular/angular.js"></script>
_x000D_