Class WFileUpload


public class WFileUpload
extends WWebWidget
A widget that allows a file to be uploaded.

This widget is displayed as a box in which a filename can be entered and a browse button.

Depending on availability of JavaScript, the behaviour of the widget is different, but the API is designed in a way which facilitates a portable use.

When JavaScript is available, the file will not be uploaded until upload() is called. This will start an asynchronous upload (and thus return immediately). When no JavaScript is available, the file will be uploaded with the next click event. Thus, upload() has no effect – the file will already be uploaded, and the corresponding signals will already be emitted. To test if upload() will start an upload, you may check using the canUpload() call.

Thus, to properly use the widget, one needs to follow these rules:

The WFileUpload widget must be hidden or deleted when a file is received. In addition it is wise to prevent the user from uploading the file twice as in the example below.

The uploaded file is automatically spooled to a local temporary file which will be deleted together with the WFileUpload widget, unless stealSpooledFile() is called.

WFileUpload is an inline widget.

CSS

The file upload itself corresponds to a <input type="file"> tag, but may be wrapped in a <form> tag for an Ajax session to implement the asynchronous upload action. This widget does not provide styling, and styling through CSS is not well supported across browsers.

  • Constructor Details

  • Method Details

    • remove

      public void remove()
      Description copied from class: WWidget
      Destructor.

      Deletes a widget and all contained contents.

      Overrides:
      remove in class WWebWidget
      See Also:
      WWidget.removeWidget(WWidget widget)
    • setMultiple

      public void setMultiple​(boolean multiple)
      Sets whether the file upload accepts multiple files.

      In browsers which support the "multiple" attribute for the file upload (to be part of HTML5) control, this will allow the user to select multiple files at once.

      All uploaded files are available from getUploadedFiles(). The single-file API will return only information on the first uploaded file.

      The default value is false.

    • isMultiple

      public boolean isMultiple()
      Returns whether multiple files can be uploaded.

      See Also:
      setMultiple(boolean multiple)
    • setFileTextSize

      public void setFileTextSize​(int chars)
      Sets the size of the file input.
    • getFileTextSize

      public int getFileTextSize()
      Returns the size of the file input.
    • getSpoolFileName

      public java.lang.String getSpoolFileName()
      Returns the spooled location of the uploaded file.

      Returns the temporary filename in which the uploaded file was spooled. The file is guaranteed to exist as long as the WFileUpload widget is not deleted, or a new file is not uploaded.

      When multiple files were uploaded, this returns the information from the first file.

      See Also:
      stealSpooledFile(), uploaded()
    • getClientFileName

      public java.lang.String getClientFileName()
      Returns the client filename.

      When multiple files were uploaded, this returns the information from the first file.

      Note: Depending on the browser this is an absolute path or only the file name.

    • getContentDescription

      public java.lang.String getContentDescription()
      Returns the client content description.

      When multiple files were uploaded, this returns the information from the first file.

    • stealSpooledFile

      public void stealSpooledFile()
      Steals the spooled file.

      By stealing the file, the spooled file will no longer be deleted together with this widget, which means you need to take care of managing that.

      When multiple files were uploaded, this returns the information from the first file.

    • isEmpty

      public boolean isEmpty()
      Returns whether one or more files have been uploaded.
    • getUploadedFiles

      public java.util.List<UploadedFile> getUploadedFiles()
      Returns the uploaded files.
    • canUpload

      public boolean canUpload()
      Returns whether upload() will start a new file upload.

      A call to upload() will only start a new file upload if there is no JavaScript support. Otherwise, the most recent file will already be uploaded.

    • setDisplayWidget

      public void setDisplayWidget​(WInteractWidget widget)
      Use the click signal of another widget to open the file picker.

      This hides the default WFileUpload widget and uses the click-signal of the argument to open the file picker. The upload logic is still handled by WFileUpload behind the scenes. This action cannot be undone.

      WFileUpload does not take ownership of the widget, nor does it display it. You must still place it in the widget tree yourself.

    • uploaded

      public EventSignal uploaded()
      Signal emitted when a new file was uploaded.

      This signal is emitted when file upload has been completed. It is good practice to hide or delete the WFileUpload widget when a file has been uploaded succesfully.

      See Also:
      upload(), fileTooLarge()
    • fileTooLarge

      public JSignal1<java.lang.Long> fileTooLarge()
      Signal emitted when the user tried to upload a too large file.

      The parameter is the (approximate) size of the file (in bytes) the user tried to upload.

      The maximum file size is determined by the maximum request size, which may be configured in the configuration file (<max-request-size>).

      See Also:
      uploaded(), WApplication.requestTooLarge()
    • changed

      public EventSignal changed()
      Signal emitted when the user selected a new file.

      One could react on the user selecting a (new) file, by uploading the file immediately.

      Caveat: this signal is not emitted with konqueror and possibly other browsers. Thus, in the above scenario you should still provide an alternative way to call the upload() method.

    • upload

      public void upload()
      Starts the file upload.

      The uploaded() signal is emitted when a file is uploaded, or the fileTooLarge() signal is emitted when the file size exceeded the maximum request size.

      See Also:
      uploaded(), canUpload()
    • setProgressBar

      public void setProgressBar​(WProgressBar bar)
      Sets a progress bar to indicate upload progress.

      When the file is being uploaded, upload progress is indicated using the provided progress bar. Both the progress bar range and values are configured when the upload starts.

      The file upload itself is hidden as soon as the upload starts.

      The default progress bar is 0 (no upload progress is indicated).

      To update the progess bar server push is used, you should only use this functionality when using a Servlet 3.0 compatible servlet container.

      See Also:
      dataReceived()
    • getProgressBar

      public WProgressBar getProgressBar()
      Returns the progress bar.

      See Also:
      setProgressBar(WProgressBar bar)
    • dataReceived

      public Signal2<java.lang.Long,​java.lang.Long> dataReceived()
      Signal emitted while a file is being uploaded.

      When supported by the connector library, you can track the progress of the file upload by listening to this signal.

      The first argument is the number of bytes received so far, and the second argument is the total number of bytes.

    • enableAjax

      public void enableAjax()
      Description copied from class: WWidget
      Progresses to an Ajax-enabled widget.

      This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

      You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

      Overrides:
      enableAjax in class WWebWidget
      See Also:
      WApplication.enableAjax()
    • setFilters

      public void setFilters​(java.lang.String acceptAttributes)
      Sets input accept attributes.

      The accept attribute may be specified to provide user agents with a hint of what file types will be accepted. Use html input accept attributes as input.

      
       WFileUpload *fu = new WFileUpload(root());
       fu.setFilters("image/*");
      
      
       
    • createDomElement

      protected DomElement createDomElement​(WApplication app)
      Description copied from class: WWebWidget
      Create DOM element for widget.

      This is an internal function, and should not be called directly, or be overridden!

      Overrides:
      createDomElement in class WWebWidget
    • getDomChanges

      protected void getDomChanges​(java.util.List<DomElement> result, WApplication app)
      Description copied from class: WWebWidget
      Get DOM changes for this widget.

      This is an internal function, and should not be called directly, or be overridden!

      Overrides:
      getDomChanges in class WWebWidget
    • propagateSetEnabled

      protected void propagateSetEnabled​(boolean enabled)
      Description copied from class: WWidget
      Propagates that a widget was enabled or disabled through children.

      When enabling or disabling a widget, you usually also want to disable contained children. This method is called by setDisabled() to propagate its state to all children.

      You may want to reimplement this method if they wish to render differently when a widget is disabled. The default implementation will propagate the signal to all children.

      Overrides:
      propagateSetEnabled in class WWebWidget
    • setFormData

      protected void setFormData​(WObject.FormData formData)
      Overrides:
      setFormData in class WObject