Recently, I've started working with AngularJs and I'm facing an issue with uploading image files along with their labels through a Jax-RS post rest API. My problem is that the control in my AngularJS controller is not entering the Java post API.
During debugging, I noticed that the control is not reaching the Java file. Can someone please assist me in figuring out what might be wrong in my code?
myfile.html
Label of File: <input type="text" name="iconlabel" data-ng-model="imporLabelName"><br>
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
<button type="button" class="btn btn-primary pull-right" data-ng-click="uploadfile();">Submit
</button>
myFileController
define([ '{angular}/angular' ], function(angular) {
var module = angular.module('myFile', [ 'ngResource', 'colorpicker-dr' ]);
module.controller('myFileController', [ '$scope', '$sce', '$http', '$location', '$window', 'w20MessageService'
,function($scope, $sce, $http, $location, $window, w20MessageService) {
var config = {
headers: {
"Content-Type": undefined,
}
};
/*** Modale for MyFile **/
$scope.openMyFile = function() {
$("#myfile").modal("show");
};
$scope.uploadfile = function() {
$scope.file = document.getElementById('myFile').files[0];
alert('LabelName = '+$scope.imporLabelName);
var formData = new $window.FormData();
formData.append("label", $scope.imporLabelName);
formData.append("file", $scope.file);
alert('File = '+$scope.file);
var url = "uploadfile/label="+$scope.imporLabelName;
alert("URL = "+url);
$http.post(url,$scope.formData,config).success(function(response) {
$scope.result = "SUCCESS";
}).error(function(response, status) {
if (status === 400) {
w20MessageService.addMessageErreur(data.message, "msgGererParam");
} else {
w20MessageService.addMessageErreur("eox.message.error.load.traitement", "msgGererParam");
}
});
$("#myfile").modal("hide");
};
} ]);
return {
angularModules : [ 'digitalJes' ]
};
});
java api code
@POST
@Path("/uploadfile/{label}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@PathParam("label") String label, @FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInputDetails) {
CacheControl cc = new CacheControl();
cc.setNoCache(true);
return Response.ok(uploadFileUtil.uploadFile(label, fileInputStream, fileInputDetails)).cacheControl(cc).build();
}
Error Code and Error Message
HTTP Status 400 – Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).