Announcement Announcement Module
Collapse
No announcement yet.
Remembering multipart file upload on form validation and reloads Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remembering multipart file upload on form validation and reloads

    Hi, i've just started using the Spring framework a couple of weeks ago, everything was going fine, until i started using the Multipart file upload utilities included in Spring.

    I have a SimpleFormController and a Command with one String field and another byte[] field. The form, as u might have guessed, has an input:text and an input:file. Evertything works fine, the file gets uploaded into the byte[] field of my command...the problem is when i validate both fields.

    With a class implementing the Validator interface, i'm validating that both the text field and the byte[] are not empty. I use the bind tag to show in the form the previous value of the text field...the problem comes with the input:file.

    For security reasons, the browser won't let me set the previous value the input:file had, the only way to set this value is through the "Explore" button.

    So for example, if the first time I submit the form, the text is empty, but the file is ok, the form is shown again with the error pointing that the text should not me empty...If i correct the text and resubmit the form, since the input:file won't remember the path, the form is shown again with the error pointint that now the file is the one that's empty...this shouldn't be, since the first time the form was submitted, the file was ok.

    If the file was uploaded ok the first time, is there a Spring compliant way to not bind the input:file again if it's resubmited empty, but had content before?

    Any ideas anybody? Thanks in advance

  • #2
    I think you have a couple of opertions, keep the session on the form (probably with a temporary file to store the contents of the file), or split the form into two sequences, i.e. do the upload last.

    HTH.

    Comment


    • #3
      Thanx for the advice...but after looking at the program flow on all the controller classes, i found the "Spring compliant way" to do this...all that needs to be done is on the controller, override the initBinder() method and on the binder object set the bindEmptyMultipartFiles to false...this lets the binder know to ignore multipart files that are empty.

      protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
      binder.setBindEmptyMultipartFiles(false);
      binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
      }


      Works perfectly!

      Comment


      • #4
        not quite

        I presume you are storing your command on the session... If not, then in the scenario you mentioned, the second time you upload the form, the file will still be null...

        Comment


        • #5
          I've tested it a couple of times and it works.

          Of course i'm saving the command in the session or i would loose my file every time. My problem was when there was an error on some other field, if i resubmited the form, the binding process made my byte[] null, even if it was ok on the previous submit, but now everything works OK.

          Comment

          Working...
          X