
I have a file uploading code but it create problem
$(document).ready(function(){
$('#upload').click(function(){
var fileName=$('#myFile').val();
alert(fileName);
$.ajax({
type: 'POST',
dataType: 'json',
url: 'myServlet',
data:{path:fileName},
contentType:'multipart/form-data'
success: function(msg){
//alert( "Data Saved: " + msg );
document.getElementById('filenameSeq').innerHTML='<div style="width:200px; color: black;background-color: white;"><ul><li>'+msg+'  <a href="#####"><b>X</b></a></li></ul></div>';
}
});
});
});
but when we run it the code it gives the problem
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
and my server side code is
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
System.out.println("------------------------------------------");
FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(request);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List /* FileItem */ items = (List) upload.parseRequest(request);
Iterator iter = (Iterator) ((java.util.List) items).iterator();
while (((java.util.Iterator) iter).hasNext()) {
FileItem item = (FileItem) ((java.util.Iterator) iter).next();
if (item.isFormField()) {
System.out.println("its a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/Web-INF/manu/"),cfile.getName());
System.out.println(tosave);
item.write(tosave);
}
}
}catch(Exception e){System.out.println(e);}
}
this code run perfectly well when i send action without jquery
if u have any solution then share it
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.