Announcement Announcement Module
No announcement yet.
Binding to File Input Type Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding to File Input Type

    Well, I think I'm pretty close to having my image upload form complete, however, I'm trying to figure out how I can maintain the uploaded image if validation errors occur upon form submit.

    Basically, I do not want the end-users thinking that they must re-upload their image each time form validation fails. It would be nice if the uploading process does not occur if form validation errors occur, and even nicer if the image upload field maintained the path selected by the user.

    Anyhow, here's the code I have so far:

    In my Controller:

    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    		binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    		super.initBinder(request, binder);
    In my JSP:

            <spring&#58;bind path="command.imageOne">
              <input type="file" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="<c&#58;out value="$&#123;status.value&#125;"/>">
    Also, in my command model, imageOne is defined as a byte[].

    Any help is greatly appreciated!



  • #2
    A couple of points:

    - the HTML spec prevents the setting of the file upload control for security purposes (imagine adding a hidden one preset with some critical file )
    - the only way to *not* do fileupload on validation is to do them on two seperate pages.
    - if you want to keep the user's file data between requests, you can store your formBackingObject on the session. You can then on the jsp check if the formBackingObject.yourFile is empty or not.

    Also, what is the purpose of the "value" element on the file input type?


    • #3
      Binding to File Input Type


      Thank you very much for your reply, I more than appreciate it.

      Putting the value element in the HTML tag was my futile attempt in retrieving the full path name to the image, however, per your point below, this is obviously not an option, due to the HMTL spec.

      Again, thanks for your reply.