I use the ajax and rest web service to upload files to the server. I grabbed the file upload event and got the file in my jquery method. I need to send an object file to a web service so that I can save the file in 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) {
}
});
}
I also tried using FormData, but I could not get the file in the 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) {
}
});
Here is my java code in the 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 the Object file directly without using formData, I get "media type not supported", and if I send formData for maintenance, I get only a temporary file. What data type should be present in ajax and in service methods? How can I upload the downloaded file to the service? Any help would be appreciated.