I have the ajax script below and in servlet I am trying to read File uploaded but I am getting the below error in servlet
the request was rejected because no multipart boundary was found Below is my code
$("#form").submit(function(e){
$.ajax({
type : 'POST', // GET or POST or PUT or DELETE
url : "myServlet",
data:$('#fomr').serialize(),
contentType:"multipart/form-data",
success : function(data) {// On Successfull
},
error : function(msg) {// When Service call
}
});
});
Below is the form
<form id="fomr" >
<input name="file1" id="file1" type="file" accept="image/*" >
</form>
Below is the servlet code
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
log.info("Number of fields: " + fields.size() + "<br/><br/>");
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext()) {
log.info("No fields found");
return;
}
while (it.hasNext()) {
FileItem fileItem = it.next();
boolean isFormField = fileItem.isFormField();
if (isFormField) {
log.info("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() +
"<br/>STRING: " + fileItem.getString()
);
log.info("</td>");
} else {
log.info("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName() +
"<br/>STRING: " + fileItem.getString() +
"<br/>NAME: " + fileItem.getName() +
"<br/>CONTENT TYPE: " + fileItem.getContentType() +
"<br/>SIZE (BYTES): " + fileItem.getSize() +
"<br/>TO STRING: " + fileItem.toString()
);
}
}
} catch (FileUploadException e) {
log.error("Error {}",e);
e.printStackTrace();
}