Class UploadManager
- All Implemented Interfaces:
Serializable
UploadManager instance that UI components can
connect to.
Unlike the Upload component which provides a complete upload UI, this
manager is invisible and UI components connect to it by setting this manager
as their target. This allows more freedom in designing the upload experience.
For upload progress monitoring (start, progress, success, failure events),
use an UploadHandler that implements
TransferProgressAwareHandler.
Example usage:
// Create the manager with an upload handler
var manager = new UploadManager(this,
UploadHandler.inMemory((metadata, data) -> {
// Process uploaded file
}));
// Create UI components linked to the manager
var dropZone = new UploadDropZone(manager);
var addButton = new UploadButton(manager);
var fileList = new UploadFileList(manager);
// Add UI components to the layout
add(dropZone, addButton, fileList);
- Author:
- Vaadin Ltd.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEvent fired when all uploads have finished (either successfully, failed, or aborted).static classEvent fired when a file is rejected by the upload manager due to constraints like max file size, max files, or accepted file types.static classEvent fired when a file is removed from the upload manager. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe feature flag ID for modular upload components (UploadManager and related components). -
Constructor Summary
ConstructorsConstructorDescriptionUploadManager(com.vaadin.flow.component.Component owner) Creates a new upload manager without an upload handler.UploadManager(com.vaadin.flow.component.Component owner, com.vaadin.flow.server.streams.UploadHandler handler) Creates a new upload manager with the given upload handler. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddAllFinishedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.AllFinishedEvent> listener) Add a listener that is informed when all uploads have finished.com.vaadin.flow.shared.RegistrationaddFileRejectedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.FileRejectedEvent> listener) Adds a listener forfile-rejectevents fired when a file cannot be added due to some constraints:setMaxFileSize, setMaxFiles, setAcceptedFileTypescom.vaadin.flow.shared.RegistrationaddFileRemovedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.FileRemovedEvent> listener) Adds a listener for events fired when a file is removed.voidClear the list of files being processed, or already uploaded.Get the list of accepted file types for upload.intGet the maximum number of files allowed for the user to select to upload.longGet the maximum allowed file size in the client-side, in bytes.booleanGet the auto upload status.booleanGets whether the upload manager is enabled.booleanChecks if an upload is currently in progress.voidsetAcceptedFileTypes(String... acceptedFileTypes) Specify the types of files that the upload accepts.voidsetAutoUpload(boolean autoUpload) Whenfalse, it prevents uploads from triggering immediately upon adding file(s).voidsetEnabled(boolean enabled) Sets whether the upload manager is enabled.voidsetMaxFiles(int maxFiles) Limit of files to upload, by default it is unlimited.voidsetMaxFileSize(long maxFileSize) Specify the maximum file size in bytes allowed to upload.voidsetUploadHandler(com.vaadin.flow.server.streams.UploadHandler handler) Sets the upload handler that processes uploaded files.voidsetUploadHandler(com.vaadin.flow.server.streams.UploadHandler handler, String targetName) Sets the upload handler that processes uploaded files.
-
Field Details
-
FEATURE_FLAG_ID
The feature flag ID for modular upload components (UploadManager and related components).- See Also:
-
-
Constructor Details
-
UploadManager
public UploadManager(com.vaadin.flow.component.Component owner) Creates a new upload manager without an upload handler. The handler must be set usingsetUploadHandler(UploadHandler)before uploads can work.- Parameters:
owner- the component that owns this manager. The manager's lifecycle is tied to the owner's lifecycle - when the owner is detached from the UI, uploads will stop working. The owner is typically the view or layout containing the upload UI components.
-
UploadManager
public UploadManager(com.vaadin.flow.component.Component owner, com.vaadin.flow.server.streams.UploadHandler handler) Creates a new upload manager with the given upload handler.- Parameters:
owner- the component that owns this manager. The manager's lifecycle is tied to the owner's lifecycle - when the owner is detached from the UI, uploads will stop working. The owner is typically the view or layout containing the upload UI components.handler- the upload handler to use
-
-
Method Details
-
setUploadHandler
public void setUploadHandler(com.vaadin.flow.server.streams.UploadHandler handler) Sets the upload handler that processes uploaded files.This overload uses the default upload target name
"upload", which becomes the last segment of the dynamically generated upload URL.- Parameters:
handler- the upload handler, notnull
-
setUploadHandler
public void setUploadHandler(com.vaadin.flow.server.streams.UploadHandler handler, String targetName) Sets the upload handler that processes uploaded files.- Parameters:
handler- the upload handler, notnulltargetName- the endpoint name (single path segment), used as the last path segment of the dynamically generated upload URL; must not be blank
-
setMaxFiles
public void setMaxFiles(int maxFiles) Limit of files to upload, by default it is unlimited. If the value is set to one, the native file browser will prevent selecting multiple files.- Parameters:
maxFiles- the maximum number of files allowed for the user to select, or 0 for unlimited
-
getMaxFiles
public int getMaxFiles()Get the maximum number of files allowed for the user to select to upload.- Returns:
- the maximum number of files, or 0 if unlimited
-
setMaxFileSize
public void setMaxFileSize(long maxFileSize) Specify the maximum file size in bytes allowed to upload. Notice that it is a client-side constraint, which will be checked before sending the request.- Parameters:
maxFileSize- the maximum file size in bytes, or 0 for unlimited
-
getMaxFileSize
public long getMaxFileSize()Get the maximum allowed file size in the client-side, in bytes.- Returns:
- the maximum file size in bytes, or 0 if unlimited
-
setAcceptedFileTypes
Specify the types of files that the upload accepts. Syntax: a MIME type pattern (wildcards are allowed) or file extensions. Notice that MIME types are widely supported, while file extensions are only implemented in certain browsers, so it should be avoided.Example:
"video/*","image/tiff"or".pdf","audio/mp3"- Parameters:
acceptedFileTypes- the allowed file types to be uploaded, ornullto clear any restrictions
-
getAcceptedFileTypes
Get the list of accepted file types for upload.- Returns:
- a list of allowed file types, never
null
-
setAutoUpload
public void setAutoUpload(boolean autoUpload) Whenfalse, it prevents uploads from triggering immediately upon adding file(s). The default istrue.- Parameters:
autoUpload-trueto allow uploads to start immediately after selecting files,falseotherwise
-
isAutoUpload
public boolean isAutoUpload()Get the auto upload status.- Returns:
trueif the upload of files should start immediately after they are selected,falseotherwise
-
setEnabled
public void setEnabled(boolean enabled) Sets whether the upload manager is enabled. When disabled, uploads cannot be started from any linked UI components (buttons, drop zones).This is the authoritative server-side control for preventing uploads. Disabling individual UI components only affects the UI but does not prevent a malicious client from initiating uploads. Use this method to securely prevent uploads.
- Parameters:
enabled-trueto enable uploads,falseto disable
-
isEnabled
public boolean isEnabled()Gets whether the upload manager is enabled.- Returns:
trueif uploads are enabled,falseotherwise
-
isUploading
public boolean isUploading()Checks if an upload is currently in progress.- Returns:
trueif receiving upload,falseotherwise
-
clearFileList
public void clearFileList()Clear the list of files being processed, or already uploaded. -
addFileRemovedListener
public com.vaadin.flow.shared.Registration addFileRemovedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.FileRemovedEvent> listener) Adds a listener for events fired when a file is removed.- Parameters:
listener- the listener- Returns:
- a
Registrationfor removing the event listener
-
addFileRejectedListener
public com.vaadin.flow.shared.Registration addFileRejectedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.FileRejectedEvent> listener) Adds a listener forfile-rejectevents fired when a file cannot be added due to some constraints:setMaxFileSize, setMaxFiles, setAcceptedFileTypes- Parameters:
listener- the listener- Returns:
- a
Registrationfor removing the event listener
-
addAllFinishedListener
public com.vaadin.flow.shared.Registration addAllFinishedListener(com.vaadin.flow.component.ComponentEventListener<UploadManager.AllFinishedEvent> listener) Add a listener that is informed when all uploads have finished.- Parameters:
listener- all finished listener to add- Returns:
- a
Registrationfor removing the event listener
-