-1

Hi I have a form having upload file and an input type="text".

<input type="file" id="uploadFile" name="file" required="required">
<input type="text" id="purpose"  name="purpose" required="required">

Now when the user clicks the button I want to hit a servlet through AJAX and set the file to a required folder and also retrieve the purpose input value and put it in my table. Any help how can I achieve this ?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Uday Khatry
  • 449
  • 1
  • 8
  • 23
  • 2
    You cannot upload files using AJAX unless you use HTML 5 or send files using hidden iFrame(s). Furthermore, the Servlet API does not support the facility to parse a multipart request out of the box (if you otherwise want to send multipart contents to upload files). You will need extra dependencies like Apache Commons FileUpload and its dependency Apache Commons IO (and optionally Apache Commons Logging). – Tiny Dec 19 '14 at 19:28
  • Actually if you are using the latest Servlet API it does support it out of the box via getPart...but I would still suggest Apache Commons File Upload library because its just easier and works on older setups too. As to this question, there are hundreds like it on SO. Search around a bit. Shouldn't be hard to find. – developerwjk Dec 19 '14 at 21:17
  • http://stackoverflow.com/questions/6914152/how-to-send-a-file-to-a-servlet-via-ajax?rq=1 - why duplicate a question and a perfectly valid answer? – Alex Pakka Dec 20 '14 at 08:36

1 Answers1

0

I encountered with this problem yesterday. As developerwjk said, I used:

  • commons-fileupload.jar
  • commons-io.jar

For more information see this useful question.

Community
  • 1
  • 1
Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27