Search code examples
javajavascriptservletsuploadwebsphere

Slow file upload: Can I remove file attachment from multipart POST data on the fly and just submit form text fields?


I have a regular JSP/Servlet/Java web application that is used for uploading pictures from a mobile device. I am using Apache Commons library for the upload. Application is hosted on WebSphere Application Server 7.0.

Everything is working fine and the user can upload several images totaling 8MB or more if he has a really good/strong signal/connection or on a good WiFi.

The problem arises when the user is at a location with poor 3G/4G signal/connection. He gets errors like "Illegal state exception" or some time-out error, and in some cases the mobile browser just stays on the submit page with the progress bar no longer moving.

Any suggestions on how to "gracefully" handle this? Like is there a way to intervene after a set amount of time and give the the user an option to submit the form without the file attachment (i.e. just submit the form text fields)? Any other suggestions are welcome too.

UPDATE: The setTimeout solution below worked for me. The other missing piece was that I have to issue a "browser stop" command to stop the original submission that's in progress before I can issue a re-submit. Otherwise, my re-submit command will just be ignored by the browser.


Solution

  • The usecase here is simple - if the upload didn't finish in N minutes, remove/clear the field using javascript and resend the form.

    You don't need to control the upload in the basic implementation, just safely asume that if you set a timeout to resend, it won't happen if the first attempt was successful and the page reloaded.

    jQuery pseudocode:

    setTimeout(function(){
      $imageFieldNode.remove();
      $form.trigger('submit');
    },30000);//after 30 seconds
    

    The more advanced way is to use a ready solution for controlled upload. They work like that:

    1. upload starts
    2. js prompts the server in intervals with a GET query to get the size of content that was already received.
    3. everytime it gets the info - it reports progress.

    You can do a lot with these libs.