0

I am having formData that contains a file

Like :

  var files = document.getElementById("uploadfile").files;
  /* Create a FormData instance */
  var formData = new FormData();
  /* Add the file */ 
  var file = files[0];
  alert(file.name);
  formData.append('uploadfile', file, file.name);
  client.open("POST", "fileupload?q="+uploadfile,true);
  client.setRequestHeader("Content-Type", "multipart/form-data");
  client.send(formData);  /* Send to server */ 

Now i want to use this form data in my servlet fileupload.How to use it their ?Please help.

user3462609
  • 105
  • 2
  • 11

1 Answers1

0

I am giving an example how I handled it in my project. I used doPost function to get it.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

Do whatever you want with that item.

}
ahmet aydin
  • 116
  • 2
  • 10
  • It gives an exception that org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found – user3462609 Mar 31 '14 at 19:09
  • http://stackoverflow.com/a/2424824/2599879 this answer also explains in detail how to handle file upload. You can find also necessary jar links in the answer which are commons-fileupload.jar and commons-io.jar – ahmet aydin Mar 31 '14 at 19:58