File upload/download servlet code from reactjs

Home / Uncategorized / File upload/download servlet code from reactjs

Question:
Am getting "isMultipart" is false while am pushing file from ajax to servlet. Could anyone help me out on this file upload/download functionality. Am writting ajax in reactjs.

Ajax call request for file upload API :upload(data) {
ajax({
url: ‘api/UploadDocServlet’,
data: data,
enctype: ‘multipart/form-data’,
cache: false,
processData: false,
contentType: false,
method:’POST’,
}, (data) => {
console.log("Print data::::"+data);
if (data.error == null) {
this.search({});
} else {
alert(data.error);
}
});
}

Servlet code for file upload :boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// process only if it is multipart content
if (isMultipart) {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try {
System.out.println("UploadDocServlet:::" + upload.parseRequest(request));
List multiparts = upload.parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
String name = new File(item.getName()).getName();
Object result = service.uploadDoc(name);
//item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
}
}


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *