To accomplish this task, I personally handled the conversion process myself. In JavaScript, I first convert my file into base64 and then further convert it into a byte array in the Spring controller using javax.xml.bind.DatatypeConverter.parseBase64Binary().
Here is the custom JavaScript code for manually converting your file into base64:
$scope.onFileSelect = function(ele){
$scope.files = ele.files;
var reader = new FileReader();
reader.onload = function(e){
console.log("about to encode");
$scope.encoded_file = btoa(e.target.result.toString());
};
reader.readAsBinaryString($scope.files[0]);
$scope.fileBase64 = new FileReader();
$scope.fileBase64.readAsDataURL($scope.files[0]);
console.log("$scope.fileBase64: ",$scope.fileBase64);
};
You should also include the on-change attribute in your file input HTML like so:
<input id="Data" type="file" multiple onchange="angular.element(this).scope().onFileSelect(this)">
In the Spring controller itself, simply change the request file to String that is encapsulated within one entity:
@RequestBody PositionNew positionNew
The PositionNew entity contains attributes:
private String base64File;
private String attr1;
private String attr2;
That's all.