After completing the AngularJS part of the file upload, I encountered an error when trying to send the uploaded file to my controller. The error message indicated that the URL was not valid:
The code for my controller is as follows:
@RestController
@RequestMapping("/files")
public class UploadController {
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@Produces(MediaType.APPLICATION_JSON)
public Data continueFileUpload(HttpServletRequest request, HttpServletResponse response){
MultipartHttpServletRequest mRequest;
....
}
Below is the AngularJS service that links to the controller:
AngularJS controller function snippet:
$scope.uploadFile = function() {
var file = $scope.myFile;
console.log('file is ');
console.dir(file);
var uploadUrl = "/fileUpload"; /* At this point, I needed to determine the correct URL to set */
FileUploadService.uploadFileToUrl(file, uploadUrl).then(
function(result) {
$scope.errors = FileUploadService.getResponse();
console.log($scope.errors);
$scope.errVisibility = true;
}, function(error) {
alert('error');
})
}
The corresponding FileUploadService script in JS:
myapp.service('FileUploadService', [ '$q', '$http', function($q, $http) {
var deffered = $q.defer();
var responseData;
this.uploadFileToUrl = function(file, uploadUrl) {
var fd = new FormData();
fd.append('file', file);
return $http.post(uploadUrl, fd, {
transformRequest : angular.identity,
headers : {
'Content-Type' : undefined
}
}).success(function(response) {
/* $scope.errors = response.data.value; */
console.log(response);
responseData = response;
deffered.resolve(response);
return deffered.promise;
}).error(function(error) {
deffered.reject(error);
return deffered.promise;
});
}
this.getResponse = function() {
return responseData;
}
} ]);
I am uncertain about which specific URl I should provide to call the continueFileUpload function.
Here is an updated insight:
The aforementioned itr appears to be loaded without any content.
The directive I utilized is as follows:
Here is a glimpse at the HTML structure used: