I am trying to upload files along with some other form fields using jQuery AJAX calls.
This is a common 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);
});
}
On examining from the dev tools from browsers, these are the respective request contents:
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 server side, we are parsing 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)) {
// get the request content and iterate through
items = fileUpload.parseRequest(request);
}
The code works fine from Chrome and Firefox, but throws the below exception when I tried from IE11.
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 appreciated. Thanks.
Turned out a weird issue. This is how it's resolved.
That' it. Worked like a magic!
More info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With