eu.webtoolkit.jwt
Class WFileUpload

java.lang.Object
  extended by eu.webtoolkit.jwt.WObject
      extended by eu.webtoolkit.jwt.WWidget
          extended by eu.webtoolkit.jwt.WWebWidget
              extended by eu.webtoolkit.jwt.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. This widget does not provide styling, and styling through CSS is not well supported across browsers.


Constructor Summary
WFileUpload()
          Creates a file upload widget.
WFileUpload(WContainerWidget parent)
          Creates a file upload widget.
 
Method Summary
 boolean canUpload()
          Returns whether upload() will start a new file upload.
 EventSignal changed()
          Signal emitted when the user selected a new file.
 Signal2<java.lang.Long,java.lang.Long> dataReceived()
          Signal emitted while a file is being uploaded.
 void enableAjax()
          Progresses to an Ajax-enabled widget.
 Signal1<java.lang.Long> fileTooLarge()
          Signal emitted when the user tried to upload a too large file.
 java.lang.String getClientFileName()
          Returns the client filename.
 java.lang.String getContentDescription()
          Returns the client content description.
 int getFileTextSize()
          Returns the size of the file input.
 WProgressBar getProgressBar()
          Returns the progress bar.
 java.lang.String getSpoolFileName()
          Returns the spooled location of the uploaded file.
 java.util.List<UploadedFile> getUploadedFiles()
          Returns the uploaded files.
 boolean isEmpty()
          Returns whether one or more files have been uploaded.
 boolean isEmptyFileName()
          Deprecated. This method was renamed to isEmpty()
 boolean isMultiple()
          Returns whether multiple files can be uploaded.
 void remove()
          Destructor.
 void setFileTextSize(int chars)
          Sets the size of the file input.
 void setMultiple(boolean multiple)
          Sets whether the file upload accepts multiple files.
 void setProgressBar(WProgressBar bar)
          Sets a progress bar to indicate upload progress.
 void stealSpooledFile()
          Steals the spooled file.
 void upload()
          Starts the file upload.
 EventSignal uploaded()
          Signal emitted when a new file was uploaded.
 
Methods inherited from class eu.webtoolkit.jwt.WWebWidget
addStyleClass, callJavaScriptMember, childrenChanged, doJavaScript, domCanBeSaved, escapeText, escapeText, escapeText, escapeText, find, getAttributeValue, getChildren, getClearSides, getDecorationStyle, getFloatSide, getHeight, getId, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getOffset, getPositionScheme, getStyleClass, getTabIndex, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isRendered, isVisible, jsStringLiteral, jsStringLiteral, load, propagateSetEnabled, refresh, removeScript, removeStyleClass, resize, setAttributeValue, setClearSides, setDecorationStyle, setDisabled, setFloatSide, setHidden, setHiddenKeepsGeometry, setId, setInline, setJavaScriptMember, setLineHeight, setLoadLaterWhenInvisible, setMargin, setMaximumSize, setMinimumSize, setOffsets, setPopup, setPositionScheme, setSelectable, setStyleClass, setTabIndex, setToolTip, setVerticalAlignment, updateSignalConnection
 
Methods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addStyleClass, animateHide, animateShow, boxBorder, boxPadding, containsExposed, disable, dropEvent, enable, getDrop, getJsRef, getParent, hide, htmlText, isLayoutSizeAware, layoutSizeChanged, positionAt, positionAt, removeStyleClass, resize, setClearSides, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr
 
Methods inherited from class eu.webtoolkit.jwt.WObject
getObjectName, setObjectName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WFileUpload

public WFileUpload(WContainerWidget parent)
Creates a file upload widget.


WFileUpload

public WFileUpload()
Creates a file upload widget.

Calls this((WContainerWidget)null)

Method Detail

remove

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

Deletes a widget and all children (recursively). If the widget is contained in another widget, it is removed first.

Overrides:
remove in class WWebWidget
See Also:
WContainerWidget.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.


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.


isEmptyFileName

public boolean isEmptyFileName()
Deprecated. This method was renamed to isEmpty()

Checks if no filename was given and thus no file uploaded. (Deprecated).

Return whether a non-empty filename was given.


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.


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 Signal1<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 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.

If the provided progress bar already has a parent, then the file upload itself is hidden as soon as the upload starts. If the provided progress bar does not yet have a parent, then the bar becomes part of the file upload, and replaces the file prompt when the upload is started.

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.


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()