I am trying to upload files along with some other form fields using jQuery AJAX calls.
This is a normal function that calls the URL on the server:
function uploadDocument(rquestURL,formId,callback){ $.ajax({ type : 'POST', url : rquestURL, cache:false, processData:false, contentType:false, data : new FormData($("#"+formId)[0]) }).done(function(response) { callback(response); }); }
When checking with dev tools from browsers, this is the content of the request:
From IE11
-----------------------------7dfad39402e6 Content-Disposition: form-data; name="subject" Test -----------------------------7dfad39402e6 Content-Disposition: form-data; name="message" Test test -----------------------------7dfad39402e6 Content-Disposition: form-data; name="announcementAttachment"; filename="" Content-Type: application/octet-stream <Binary File Data Not Shown> ---------------------------7dfad39402e6
Chrome
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ Content-Disposition: form-data; name="subject" Test ------WebKitFormBoundaryp8rj3ArKDsbYw0BZ Content-Disposition: form-data; name="message" Test test ------WebKitFormBoundaryp8rj3ArKDsbYw0BZ Content-Disposition: form-data; name="announcementAttachment"; filename="" Content-Type: application/octet-stream ------WebKitFormBoundaryp8rj3ArKDsbYw0BZ--
On the server side, we process the request as:
import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; final FileItemFactory factory = new DiskFileItemFactory(); final ServletFileUpload fileUpload = new ServletFileUpload(factory); if (ServletFileUpload.isMultipartContent(request)) {
This code works fine with Chrome and Firefox, but when you try IE11 throws the following exception.
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
I referred to these SO questions, but in vain.
Any useful pointers are welcome. Thanks.