Class WFileUpload
public class WFileUpload extends WWebWidget
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:
- Be prepared to handle the
uploaded()
orfileTooLarge()
signals also whenupload()
was not called. - Check using
canUpload()
ifupload()
will schedule a new upload. if (!canUpload()) thenupload()
will not have any effect. if (canUpload()
),upload()
will start a new file upload, which completes succesfully using anuploaded()
signal or afileTooLarge()
signals gets emitted.
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
Constructors Constructor Description WFileUpload()
Creates a file upload widget.WFileUpload(WContainerWidget parent)
Creates a file upload widget. -
Method Summary
Modifier and Type Method Description boolean
canUpload()
Returns whetherupload()
will start a new file upload.EventSignal
changed()
Signal emitted when the user selected a new file.protected DomElement
createDomElement(WApplication app)
Create DOM element for widget.Signal2<java.lang.Long,java.lang.Long>
dataReceived()
Signal emitted while a file is being uploaded.void
enableAjax()
Progresses to an Ajax-enabled widget.JSignal1<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.protected void
getDomChanges(java.util.List<DomElement> result, WApplication app)
Get DOM changes for this widget.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.boolean
isMultiple()
Returns whether multiple files can be uploaded.protected void
propagateSetEnabled(boolean enabled)
Propagates that a widget was enabled or disabled through children.void
remove()
Destructor.void
setDisplayWidget(WInteractWidget widget)
Use the click signal of another widget to open the file picker.void
setFileTextSize(int chars)
Sets the size of the file input.void
setFilters(java.lang.String acceptAttributes)
Sets input accept attributes.protected void
setFormData(WObject.FormData formData)
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, blurred, callJavaScriptMember, childrenChanged, doJavaScript, escapeText, escapeText, escapeText, escapeText, find, findById, focussed, getAttributeValue, getBaseZIndex, getChildren, getClearSides, getDecorationStyle, getFloatSide, getHeight, getHtmlTagName, getId, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getOffset, getPositionScheme, getScrollVisibilityMargin, getStyleClass, getTabIndex, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, hasFocus, hasStyleClass, isCanReceiveFocus, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isRendered, isScrollVisibilityEnabled, isScrollVisible, isSetFirstFocus, isThemeStyleEnabled, isVisible, jsStringLiteral, jsStringLiteral, load, parentResized, parentResized, propagateSetVisible, refresh, removeScript, removeStyleClass, render, resize, scrollVisibilityChanged, setAttributeValue, setBaseZIndex, setCanReceiveFocus, setClearSides, setDecorationStyle, setDeferredToolTip, setDisabled, setFloatSide, setFocus, setHidden, setHiddenKeepsGeometry, setHtmlTagName, setId, setInline, setJavaScriptMember, setLineHeight, setLoadLaterWhenInvisible, setMargin, setMaximumSize, setMinimumSize, setOffsets, setPopup, setPositionScheme, setScrollVisibilityEnabled, setScrollVisibilityMargin, setSelectable, setStyleClass, setTabIndex, setThemeStyleEnabled, setToolTip, setVerticalAlignment, updateSignalConnection, voidEventSignal
Methods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addJSignal, addStyleClass, animateHide, animateShow, boxBorder, boxPadding, createJavaScript, disable, dropEvent, enable, getDropTouch, getJsRef, getParent, hide, htmlText, isExposed, isLayoutSizeAware, layoutSizeChanged, needsRerender, positionAt, positionAt, removeChild, removeStyleClass, render, resize, scheduleRender, scheduleRender, scheduleRender, setClearSides, setDeferredToolTip, setFocus, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setObjectName, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr
Methods inherited from class eu.webtoolkit.jwt.WObject
addChild, getObjectName
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
WFileUpload
Creates a file upload widget. -
WFileUpload
public WFileUpload()Creates a file upload widget.
-
-
Method Details
-
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 classWWebWidget
- 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.
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. -
isEmptyFileName
public boolean isEmptyFileName()Deprecated.This method was renamed toisEmpty()
Checks if no filename was given and thus no file uploaded. (Deprecated)Return whether a non-empty filename was given.
-
getUploadedFiles
Returns the uploaded files. -
canUpload
public boolean canUpload() -
setDisplayWidget
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 byWFileUpload
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
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
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
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 thefileTooLarge()
signal is emitted when the file size exceeded the maximum request size.- See Also:
uploaded()
,canUpload()
-
setProgressBar
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
Returns the progress bar.- See Also:
setProgressBar(WProgressBar bar)
-
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 classWWebWidget
- 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
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 classWWebWidget
-
getDomChanges
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 classWWebWidget
-
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 classWWebWidget
-
setFormData
- Overrides:
setFormData
in classWObject
-
isEmpty()