简体   繁体   中英

IE 11: Error while sending Multipart Form Data request: Stream ended unexpectedly

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.

  • We had checkboxes at the end of the form. The mentioned issue was occurring when we do not select any of the checkboxes. The request was not getting formed correctly and hence server threw error.
  • Added a hidden field at the end of the form (make sure this is the last form field) and assigned some value to it.

That' it. Worked like a magic!

More info here .

I had the same problem. I was having only the id attribute and missing the name attribute in the hidden input field which gave me the below error. Issue resolved after adding the name attribute to the input hidden type field.

id="timestamp" name="timestamp"

Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly Caused by: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly

It happened to me, the problem was that there was location.reload once document was selected for upload. This stopped the stream to be parsed.

The issue you have mentioned has troubled me for a long time. I finally have a solution to the issue. IE appends an empty name form data at the end of the formData request object which gets parsed at the server and hence the error occurs.

Below was the form data request object which was sent before the fix:

-----------------------------7e3195134f056c
Content-Disposition: form-data; name="csrfToken"

8394D82F5A776708F13CDC6D4B4DE1485C1EC05625E63B2E
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ACTION"

DELETE_LOGO
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ORG_ID"

1879048492
-----------------------------7e3195134f056c
Content-Disposition: form-data; name=" 
-----------------------------7e3195134f056c--

In order to resolve the issue, added an extra hidden field at the end of the form element.

    <csrf:form name="OrgLogoEdit" METHOD="POST" ACTION="/logo" onKeyDown="" enctype="multipart/form-data" accept-charset="UTF-8">
        <INPUT TYPE = HIDDEN NAME = "<%= Control._ACTION %>" VALUE = "<%= OrganizationLogo._UPLOAD_LOGO %>">
        <INPUT TYPE = HIDDEN NAME = "<%= Control.ORG_ID %>" VALUE = "<%= organization.getId() %>">

        <div class="cropit-preview"></div>
        <input type="range" min="0" max="100" class="cropit-image-zoom-input" step="any">
        <input type="hidden" name="dummyIEField"> <!-- this dummy hidden field resolves the stream ended unexpectedly issue -->
    </csrf:form>

The request body is now sent as below which is parsed successfully:

-----------------------------7e3195134f056c
Content-Disposition: form-data; name="csrfToken"

8394D82F5A776708F13CDC6D4B4DE1485C1EC05625E63B2E
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ACTION"

DELETE_LOGO
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ORG_ID"

1879048492
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="dummyIEField"


-----------------------------7e3195134f056c--

Hope this helps. Cheers !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM