Class FileFieldSizeCheckBehavior

All Implemented Interfaces:
Serializable, IComponentAwareEventSink, IRequestListener, IComponentAwareHeaderContributor, IClusterable

public abstract class FileFieldSizeCheckBehavior extends FileFieldChangeBehavior

A Behavior that can be added to FileUploadField-s to provide a convenient and fast size checking for users using FileApi supporting browsers. Using this has the benefit that the user does not have to upload the whole file data to receive the error about it exceeding the size limit. In browsers not supporting the FileApi this class does not change or break anything (so it is safe for IE users).

Works with single or multiple enabled file inputs.

Longer explanation:
Today (2011 first half) browsers do not read the http response until they have fully written out the http request. This means that if you post a form with files in it and wicket stops reading the input because lets say the files are too big, the servlet container will still have to read and throw away the rest of the request before it can send back the response to the client. This means that a lot of user time, network bandwidth and server cpu time is wasted. Adding this behavior cures this problem for those users with a FileApi supporting browsers. This class does not replace the standard wicket size validation (this is only a convenience feature). It will still be done at upload.

Usage is very simple (similar to an AjaxButton), just add it to the field and specify what should happen when validation passes or fails

 
 final FileUploadField uploadField = new FileUploadField("file");
 
 // add our FileApi based size check, errors are reported trough a
 // feedback panel
 uploadField.add(new FileFieldSizeCheckBehaviour() {
        private static final long serialVersionUID = 7228537141239670625L;
 
        @Override
        protected void onSubmit(AjaxRequestTarget target, FileList fileList) {
                target.add(feedback);
        }
 
        @Override
        protected void onError(AjaxRequestTarget target, FileList fileList) {
                target.add(feedback);
        }
 });
 
 

Author:
akiraly
See Also:
  • Constructor Details

    • FileFieldSizeCheckBehavior

      public FileFieldSizeCheckBehavior()
      Default constructor. Calls same constructor in parent.
    • FileFieldSizeCheckBehavior

      public FileFieldSizeCheckBehavior(int maxNumOfFiles)
      Constructor. Calls same constructor in parent.
      Parameters:
      maxNumOfFiles - maximum number of files that are processed from the ajax request, non-negative
    • FileFieldSizeCheckBehavior

      protected FileFieldSizeCheckBehavior(String event, int maxNumOfFiles)
  • Method Details

    • onEvent

      protected void onEvent(AjaxRequestTarget target, FileList fileList)
      Description copied from class: FileFieldChangeBehavior
      Called during the ajax callback.
      Specified by:
      onEvent in class FileFieldChangeBehavior
      Parameters:
      target - wicket object representing the ajax response, not-null
      fileList - list of files chosen in the upload field, not-null
    • getMaxSize

      public Bytes getMaxSize()
      Retrieves the max size limit from the bound form component's parent form.
      Returns:
      max size limit in bytes
    • onBind

      protected void onBind()
      Overrides:
      onBind in class AbstractDefaultAjaxBehavior
    • getFormComponent

      protected final FormComponent<?> getFormComponent()
      Helper method to get the bound FormComponent.
      Returns:
      form component owning this behavior
    • addErrorMsg

      protected void addErrorMsg(AjaxRequestTarget target, FileList fileList)
      Adds an error message about the exceeded file size limit. By default it mimics the upload error handling of Form. In o.a.w.markup.html.form.Form look for #handleMultiPart() and #onFileUploadException().
      Parameters:
      target - wicket object representing the ajax response, not-null
      fileList - list of files chosen in the upload field, not-null
    • onSubmit

      protected abstract void onSubmit(AjaxRequestTarget target, FileList fileList)
      Called when size validation passed.
      Parameters:
      target - wicket object representing the ajax response, not-null
      fileList - list of files chosen in the upload field, not-null
    • onError

      protected abstract void onError(AjaxRequestTarget target, FileList fileList)
      Called when size validation failed.
      Parameters:
      target - wicket object representing the ajax response, not-null
      fileList - list of files chosen in the upload field, not-null