public class Upload extends AbstractComponent implements Component.Focusable, LegacyComponent
The visible component consists of a file name input box and a browse button and an upload submit button to start uploading.
The Upload component needs a java.io.OutputStream to write the uploaded data. You need to implement the Upload.Receiver interface and return the output stream in the receiveUpload() method.
You can get an event regarding starting (StartedEvent), progress (ProgressEvent), and finishing (FinishedEvent) of upload by implementing StartedListener, ProgressListener, and FinishedListener, respectively. The FinishedListener is called for both failed and succeeded uploads. If you wish to separate between these two cases, you can use SucceededListener (SucceededEvenet) and FailedListener (FailedEvent).
The upload component does not itself show upload progress, but you can use the ProgressIndicator for providing progress feedback by implementing ProgressListener and updating the indicator in updateProgress().
Setting upload component immediate with setImmediateMode(boolean)
initiates the upload as soon as a file is selected, instead of the common
pattern of file selection field and upload button.
Note! Because of browser dependent implementations of element, setting size for Upload component is not supported. For some browsers setting size may work to some extend.
Modifier and Type | Class and Description |
---|---|
static class |
Upload.ChangeEvent
Upload.ChangeEvent event is sent when the value (filename) of the upload
changes.
|
static interface |
Upload.ChangeListener
Listener for
Upload.ChangeEvent . |
static class |
Upload.FailedEvent
Upload.FailedEvent event is sent when the upload is received, but the
reception is interrupted for some reason.
|
static interface |
Upload.FailedListener
Receives events when the uploads are finished, but unsuccessful.
|
static class |
Upload.FinishedEvent
Upload.FinishedEvent is sent when the upload receives a file, regardless
of whether the reception was successful or failed.
|
static interface |
Upload.FinishedListener
Receives the events when the uploads are ready.
|
static class |
Upload.NoInputStreamEvent
FailedEvent that indicates that an input stream could not be obtained.
|
static class |
Upload.NoOutputStreamEvent
FailedEvent that indicates that an output stream could not be obtained.
|
static interface |
Upload.ProgressListener
ProgressListener receives events to track progress of upload.
|
static interface |
Upload.Receiver
Interface that must be implemented by the upload receivers to provide the
Upload component an output stream to write the uploaded data.
|
static class |
Upload.StartedEvent
Upload.StartedEvent event is sent when the upload is started to received.
|
static interface |
Upload.StartedListener
Receives the events when the upload starts.
|
static class |
Upload.SucceededEvent
Upload.SucceededEvent event is sent when the upload is received
successfully.
|
static interface |
Upload.SucceededListener
Receives events when the uploads are successfully finished.
|
Component.ErrorEvent, Component.Event, Component.Focusable, Component.Listener
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
Sizeable.Unit
DESIGN_ATTR_PLAIN_TEXT
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
Constructor and Description |
---|
Upload()
Creates a new instance of Upload.
|
Upload(String caption,
Upload.Receiver uploadReceiver) |
Modifier and Type | Method and Description |
---|---|
Registration |
addChangeListener(Upload.ChangeListener listener)
Adds a filename change event listener.
|
Registration |
addFailedListener(Upload.FailedListener listener)
Adds the upload interrupted event listener.
|
Registration |
addFinishedListener(Upload.FinishedListener listener)
Adds the upload received event listener.
|
Registration |
addProgressListener(Upload.ProgressListener listener)
Adds the upload progress event listener.
|
Registration |
addStartedListener(Upload.StartedListener listener)
Adds the upload started event listener.
|
Registration |
addSucceededListener(Upload.SucceededListener listener)
Adds the upload success event listener.
|
void |
changeVariables(Object source,
Map<String,Object> variables)
Invoked when the value of a variable has changed.
|
protected void |
fireNoInputStream(String filename,
String mimeType,
long length) |
protected void |
fireNoOutputStream(String filename,
String mimeType,
long length) |
protected void |
fireStarted(String filename,
String mimeType)
Emit upload received event.
|
protected void |
fireUpdateProgress(long totalBytes,
long contentLength)
Emits the progress event.
|
protected void |
fireUploadInterrupted(String filename,
String mimeType,
long length)
Emits the upload failed event.
|
protected void |
fireUploadInterrupted(String filename,
String mimeType,
long length,
Exception e) |
protected void |
fireUploadSuccess(String filename,
String MIMEType,
long length)
Emits the upload success event.
|
void |
focus()
Sets the focus for this component if the component is
Focusable . |
String |
getButtonCaption()
Returns the string rendered into button that fires uploading.
|
String |
getButtonStyleName()
Returns the stylename rendered into button that fires uploading.
|
long |
getBytesRead()
Gets read bytes of the file currently being uploaded.
|
Collection<?> |
getListeners(Class<?> eventType)
Returns all listeners that are registered for the given event type or one
of its subclasses.
|
Upload.Receiver |
getReceiver()
Returns the current receiver.
|
protected UploadState |
getState()
Returns the shared state bean with information to be sent from the server
to the client.
|
protected UploadState |
getState(boolean markAsDirty)
Returns the shared state for this connector.
|
protected StreamVariable |
getStreamVariable() |
int |
getTabIndex()
Gets the Tabulator index of this Focusable component.
|
long |
getUploadSize()
Returns size of file currently being uploaded.
|
void |
interruptUpload()
Interrupts the upload currently being received.
|
boolean |
isImmediateMode()
Returns the immediate mode of the upload.
|
boolean |
isUploading() |
void |
markAsDirty()
Marks that this connector's state might have changed.
|
void |
paintContent(PaintTarget target)
Paints the content of this component.
|
void |
removeChangeListener(Upload.ChangeListener listener)
Deprecated.
|
void |
removeFailedListener(Upload.FailedListener listener)
Deprecated.
|
void |
removeFinishedListener(Upload.FinishedListener listener)
Deprecated.
|
void |
removeProgressListener(Upload.ProgressListener listener)
Deprecated.
|
void |
removeStartedListener(Upload.StartedListener listener)
Deprecated.
|
void |
removeSucceededListener(Upload.SucceededListener listener)
Deprecated.
|
void |
setButtonCaption(String buttonCaption)
In addition to the actual file chooser, upload components have button
that starts actual upload progress.
|
void |
setButtonStyleName(String buttonStyleName)
In addition to the actual file chooser, upload components have button
that starts actual upload progress.
|
void |
setImmediateMode(boolean immediateMode)
Sets the immediate mode of the upload.
|
void |
setReceiver(Upload.Receiver receiver)
Sets the receiver.
|
void |
setTabIndex(int tabIndex)
Sets the Tabulator index of this Focusable component.
|
void |
startUpload()
Go into upload state.
|
void |
submitUpload()
Forces the upload the send selected file to the server.
|
addContextClickListener, addListener, addShortcutListener, addStyleName, attach, beforeClientResponse, detach, findAncestor, fireComponentErrorEvent, fireComponentEvent, getActionManager, getCaption, getComponentError, getCustomAttributes, getData, getDebugId, getDescription, getErrorMessage, getHeight, getHeightUnits, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getWidth, getWidthUnits, isCaptionAsHtml, isConnectorEnabled, isEnabled, isOrHasAncestor, isReadOnly, isRequiredIndicatorVisible, isResponsive, isVisible, readDesign, removeContextClickListener, removeListener, removeShortcutListener, removeStyleName, setCaption, setCaptionAsHtml, setComponentError, setData, setDebugId, setDescription, setDescription, setEnabled, setHeight, setHeight, setHeightUndefined, setIcon, setId, setLocale, setParent, setPrimaryStyleName, setReadOnly, setRequiredIndicatorVisible, setResponsive, setSizeFull, setSizeUndefined, setStyleName, setStyleName, setVisible, setWidth, setWidth, setWidthUndefined, writeDesign
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
isEnabled
addListener, addStyleName, addStyleNames, attach, getCaption, getDescription, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getUI, isEnabled, isVisible, readDesign, removeListener, removeStyleName, removeStyleNames, setCaption, setEnabled, setIcon, setId, setParent, setPrimaryStyleName, setStyleName, setVisible, writeDesign
addAttachListener, addDetachListener, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
getConnectorId
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeightUndefined, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthUndefined
public Upload()
public Upload(String caption, Upload.Receiver uploadReceiver)
public void changeVariables(Object source, Map<String,Object> variables)
changeVariables
in interface VariableOwner
source
- the Source of the variable change. This is the origin of the
event. For example in Web Adapter this is the request.variables
- the Mapping from variable names to new variable values.VariableOwner.changeVariables(java.lang.Object,
java.util.Map)
public void paintContent(PaintTarget target) throws PaintException
paintContent
in interface LegacyComponent
target
- Target to paint the content on.PaintException
- if the paint operation failed.public Registration addStartedListener(Upload.StartedListener listener)
listener
- the Listener to be added, not null@Deprecated public void removeStartedListener(Upload.StartedListener listener)
listener
- the Listener to be removed.public Registration addFinishedListener(Upload.FinishedListener listener)
listener
- the Listener to be added, not null@Deprecated public void removeFinishedListener(Upload.FinishedListener listener)
listener
- the Listener to be removed.public Registration addFailedListener(Upload.FailedListener listener)
listener
- the Listener to be added, not null@Deprecated public void removeFailedListener(Upload.FailedListener listener)
listener
- the Listener to be removed.public Registration addSucceededListener(Upload.SucceededListener listener)
listener
- the Listener to be added, not null@Deprecated public void removeSucceededListener(Upload.SucceededListener listener)
listener
- the Listener to be removed.public Registration addProgressListener(Upload.ProgressListener listener)
listener
- the progress listener to be added@Deprecated public void removeProgressListener(Upload.ProgressListener listener)
listener
- the progress listener to be removedpublic Registration addChangeListener(Upload.ChangeListener listener)
listener
- the Listener to add, not null@Deprecated public void removeChangeListener(Upload.ChangeListener listener)
listener
- the listener to be removedprotected void fireStarted(String filename, String mimeType)
filename
- mimeType
- protected void fireUploadInterrupted(String filename, String mimeType, long length)
filename
- mimeType
- length
- protected void fireUploadInterrupted(String filename, String mimeType, long length, Exception e)
protected void fireUploadSuccess(String filename, String MIMEType, long length)
filename
- MIMEType
- length
- protected void fireUpdateProgress(long totalBytes, long contentLength)
totalBytes
- bytes received so farcontentLength
- actual size of the file being uploaded, if knownpublic Upload.Receiver getReceiver()
public void setReceiver(Upload.Receiver receiver)
receiver
- the receiver to set.public void focus()
Focusable
.focus
in interface Component.Focusable
focus
in class AbstractComponent
FieldEvents
,
FieldEvents.FocusEvent
,
FieldEvents.FocusListener
,
FieldEvents.BlurEvent
,
FieldEvents.BlurListener
public int getTabIndex()
getTabIndex
in interface Component.Focusable
Focusable
componentComponent.Focusable.getTabIndex()
public void setTabIndex(int tabIndex)
setTabIndex
in interface Component.Focusable
tabIndex
- the tab order of this component. Indexes usually start
from 1. Zero means that default tab order should be used.
A negative value means that the field should not be
included in the tabbing sequence.Component.Focusable.setTabIndex(int)
public void startUpload()
public void interruptUpload()
public boolean isUploading()
public long getBytesRead()
public long getUploadSize()
public String getButtonCaption()
public String getButtonStyleName()
public void setButtonCaption(String buttonCaption)
In case the button text is set to null, the button is hidden. In this
case developer must explicitly initiate the upload process with
submitUpload()
.
In case the Upload is used in immediate mode using
setImmediateMode(boolean)
, the file choose (html input with type
"file") is hidden and only the button with this text is shown.
Note the string given is set as is to the button. HTML formatting is not stripped. Be sure to properly validate your value according to your needs.
buttonCaption
- text for upload components button.public void setButtonStyleName(String buttonStyleName)
buttonStyleName
- styleName for upload components button.about when the button is shown / hidden.
public void submitUpload()
In case developer wants to use this feature, he/she will most probably
want to hide the uploads internal submit button by setting its caption to
null with setButtonCaption(String)
method.
Note, that the upload runs asynchronous. Developer should use normal upload listeners to trac the process of upload. If the field is empty uploaded the file name will be empty string and file length 0 in the upload finished event.
Also note, that the developer should not remove or modify the upload in the same user transaction where the upload submit is requested. The upload may safely be hidden or removed once the upload started event is fired.
public void markAsDirty()
ClientConnector
ClientConnector.beforeClientResponse(boolean)
followed by ClientConnector.encodeState()
for all connectors that are marked as dirty and send any updated state
info to the client.markAsDirty
in interface ClientConnector
markAsDirty
in interface LegacyComponent
markAsDirty
in class AbstractClientConnector
ClientConnector.markAsDirty()
protected StreamVariable getStreamVariable()
public Collection<?> getListeners(Class<?> eventType)
AbstractClientConnector
getListeners
in class AbstractClientConnector
eventType
- The type of event to return listeners for.public void setImmediateMode(boolean immediateMode)
If the upload is in immediate mode, the file upload is started immediately after the user has selected the file.
If the upload is not in immediate mode, after selecting the file the user must click another button to start the upload.
The default mode of an Upload component is immediate.
immediateMode
- true
for immediate mode, false
for notpublic boolean isImmediateMode()
The default mode of an Upload component is immediate.
true
if the upload is in immediate mode, false
if
the upload is not in immediate modesetImmediateMode(boolean)
protected UploadState getState()
AbstractComponent
getState
in class AbstractComponent
protected UploadState getState(boolean markAsDirty)
AbstractClientConnector
getState
in class AbstractComponent
markAsDirty
- true if the connector should automatically be marked dirty,
false otherwiseAbstractClientConnector.getState()
Copyright © 2018 Vaadin Ltd. All rights reserved.