I am using ajax and rest web service to upload files to the server. I have captured the file upload event and got the file object in my jquery method. I have to send the file object to web service so that i can save the file to db. Here is my jquery code..
$scope.selectFile = function (fileObj) {
if(fileObj!=undefined){
$.ajax({
url : "/RestServices/services/uploadFile",
type : "POST",
data : fileObj,
processData: false,
contentType: false,
success : function(result) {
$scope.$apply();
},
error : function(xhr, tStatus, err) {
// alert(err);
}
});
}
I have tried using FormData also, but I couldn't get the file in web service.
var formData = new FormData();
formData.append("fileToUpload", fileObj);
if(fileObj!=undefined){
$.ajax({
url : "/RestServices/services/uploadFile",
type : "POST",
data : formData,
processData: false,
contentType: false,
success : function(result) {
$scope.$apply();
},
error : function(xhr, tStatus, err) {
// alert(err);
}
});
Here is my java code in service
@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(File formData) {
try {
System.out.println(formData.getFileName());
} catch (Exception e) {
LOGGER.error("Exception in Upload File Endpoint"+ e.getMessage());
}
}
If I send file Object directly without using formData, i am getting "media type not supported error" and if i send formData to service, i am getting temporary file only. What datatype should be there in ajax and in service methods? How can I get the uploaded file in the service? Any help will be appreciated.