I'm trying to send files which is appended to FormData using jquery ajax. After referring some of mozilla and IBM's documents, I've came up with following.
ajax code:
var sessionId = $.cookie("referenceId");
var myFormData = { sessionId: sessionId,
cipherData: cipherData, // Encrypted xml data
payslip: document.getElementById('payslip').files[0]};
var formData = new FormData();
for (var key in myFormData) {
console.log(key, myFormData[key]);
formData.append(key, myFormData[key]);
}
$.ajax({
url : 'api/rootpath/childpath',
type : 'POST',
processData: false,
contentType: false, // Here the contentType is set to false, so what should I put at @Consumes in java code
data : {
formData: formData
},
success : function(data,status) {
alert('success');
},
failure : function(data) {
}
});
Java code:
@POST
@Path("/childpath")
@Consumes(MediaType.MULTIPART_FORM_DATA) // I tried removing it, changing it to various formats, but none worked
public Response createLoan(@FormParam("cipherData") String cipherData,@FormParam("sessionId") String sessionId,
@FormParam("payslip") File payslip);
I've been trying this for a day. I do manage to receive the file by directly submitting the form of enctype="multipart/form-data"
, but I need to do it in ajax. If I look at my tomcat's log, it always gives me 415 status code when accessing api/rootpath/childpath
. I think the problem is due to different content type received when comparing with original content type. I tried changing the MediaType.
to "multipart/form-data" etc, but it failed.