Class WFileUpload
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 -
Field Summary
Fields inherited from class eu.webtoolkit.jwt.WWidget
WT_GETEXTRAMS_JS -
Constructor Summary
ConstructorsConstructorDescriptionCreates a file upload widget.WFileUpload(WContainerWidget parentContainer) Creates a file upload widget. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whetherupload()will start a new file upload.changed()Signal emitted when the user selected a new file.protected DomElementCreate DOM element for widget.Signal emitted while a file is being uploaded.voidProgresses to an Ajax-enabled widget.Signal emitted when the user tried to upload a too large file.Returns the client filename.Returns the client content description.protected voidgetDomChanges(List<DomElement> result, WApplication app) Get DOM changes for this widget.intReturns the size of the file input.Returns the progress bar.Returns the spooled location of the uploaded file.List<eu.webtoolkit.jwt.servlet.UploadedFile> Returns the uploaded files.booleanisEmpty()Returns whether one or more files have been uploaded.booleanReturns whether multiple files can be uploaded.protected voidpropagateSetEnabled(boolean enabled) Propagates that a widget was enabled or disabled through children.voidremove()Destructor.voidsetDisplayWidget(WInteractWidget widget) Use the click signal of another widget to open the file picker.voidsetFileTextSize(int chars) Sets the size of the file input.voidsetFilters(String acceptAttributes) Sets input accept attributes.protected voidsetFormData(WObject.FormData formData) voidsetMultiple(boolean multiple) Sets whether the file upload accepts multiple files.voidSets a progress bar to indicate upload progress.voidSteals the spooled file.voidupload()Starts the file upload.uploaded()Signal emitted when a new file was uploaded.Methods inherited from class eu.webtoolkit.jwt.WWebWidget
addStyleClass, beingDeleted, blurred, callJavaScriptMember, childrenChanged, doJavaScript, escapeText, escapeText, escapeText, escapeText, find, findById, focussed, formDataChanged, 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, hideToolTip, isCanReceiveFocus, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isRendered, isScrollVisibilityEnabled, isScrollVisible, isSetFirstFocus, isThemeStyleEnabled, isVisible, iterateChildren, jsStringLiteral, jsStringLiteral, load, manageWidget, parentResized, parentResized, propagateSetVisible, refresh, removeScript, removeStyleClass, render, resendFormData, resize, scrollVisibilityChanged, setAttributeValue, setBaseZIndex, setCanReceiveFocus, setClearSides, setDecorationStyle, setDeferredToolTip, setDisabled, setFlexBox, setFloatSide, setFocus, setHidden, setHiddenKeepsGeometry, setHtmlTagName, setId, setInline, setJavaScriptMember, setLineHeight, setLoadLaterWhenInvisible, setMargin, setMaximumSize, setMinimumSize, setObjectName, setOffsets, setParentWidget, setPopup, setPositionScheme, setScrollVisibilityEnabled, setScrollVisibilityMargin, setSelectable, setStyleClass, setTabIndex, setThemeStyleEnabled, setToolTip, setVerticalAlignment, showToolTip, showToolTipOnHover, unescapeText, updateSignalConnection, voidEventSignal, widgetAdded, widgetRemovedMethods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addJSignal, addStyleClass, animateHide, animateShow, applyThemeStyles, boxBorder, boxPadding, createJavaScript, disable, dropEvent, enable, getDropTouch, getJsRef, getParent, hide, htmlText, isExposed, isGlobalWidget, isLayoutSizeAware, layoutSizeChanged, needsRerender, positionAt, positionAt, positionAt, positionAt, removeFromParent, removeStyleClass, removeWidget, render, resize, scheduleRender, scheduleRender, scheduleRender, scheduleThemeStyleApply, setClearSides, setDeferredToolTip, setFocus, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, trMethods inherited from class eu.webtoolkit.jwt.WObject
getObjectName
-
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:WWidgetDestructor.Deletes a widget and all contained contents.
- Overrides:
removein classWWebWidget- See Also:
-
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:
-
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
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
WFileUploadwidget 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:
-
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
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
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
WFileUploadwidget and uses the click-signal of the argument to open the file picker. The upload logic is still handled byWFileUploadbehind the scenes. This action cannot be undone.WFileUploaddoes 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
WFileUploadwidget when a file has been uploaded succesfully.- See Also:
-
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:
-
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:
-
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.
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:
-
getProgressBar
Returns the progress bar.- See Also:
-
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:WWidgetProgresses 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:
enableAjaxin classWWebWidget- See Also:
-
setFilters
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:WWebWidgetCreate DOM element for widget.This is an internal function, and should not be called directly, or be overridden!
- Overrides:
createDomElementin classWWebWidget
-
getDomChanges
Description copied from class:WWebWidgetGet DOM changes for this widget.This is an internal function, and should not be called directly, or be overridden!
- Overrides:
getDomChangesin classWWebWidget
-
propagateSetEnabled
protected void propagateSetEnabled(boolean enabled) Description copied from class:WWidgetPropagates 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:
propagateSetEnabledin classWWebWidget
-
setFormData
- Overrides:
setFormDatain classWObject
-