On my website, users have the ability to upload images. I am using AngularJS to post the data to a specific URL via a POST request. My question is, how can I retrieve this data in a Java servlet? My goal is to save the uploaded image in a database. Is this the correct way to go about completing this task?
//Controller
app.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' + (file));
var uploadUrl = "/Angular/login";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
//Service
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);
//directive
app.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]);
});
});
}
};
}]);
How do I fetch the above posted data within a Java servlet?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}