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(Component owner) Creates a new upload manager without an upload handler.UploadManager(Component owner, UploadHandler handler) Creates a new upload manager with the given upload handler. -
Method Summary
Modifier and TypeMethodDescriptionAdd a listener that is informed when all uploads have finished.Adds a listener forfile-rejectevents fired when a file cannot be added due to some constraints:setMaxFileSize, setMaxFiles, setAcceptedMimeTypes, setAcceptedFileExtensionsAdds a listener for events fired when a file is removed.voidClear the list of files being processed, or already uploaded.Gets the list of accepted file extensions for upload.Gets the list of accepted MIME 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.voidsetAcceptedFileExtensions(String... extensions) Sets the accepted file extensions for uploads.voidsetAcceptedMimeTypes(String... mimeTypes) Sets the accepted MIME types for uploads.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(UploadHandler handler) Sets the upload handler that processes uploaded files.voidsetUploadHandler(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
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
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
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
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
-
setAcceptedMimeTypes
Sets the accepted MIME types for uploads. Only files matching these MIME types will be accepted. Wildcard patterns like"image/*"are supported.MIME types are used both as a client-side hint (to filter the file picker) and for server-side validation (to reject uploads that don't match).
If both MIME types and file extensions are configured, a file must match at least one of each (AND logic).
- Parameters:
mimeTypes- the accepted MIME types, e.g."image/*","application/pdf"; ornullto clear- Throws:
IllegalArgumentException- if any value is null, blank, or does not contain a/character
-
getAcceptedMimeTypes
Gets the list of accepted MIME types for upload.- Returns:
- a list of accepted MIME types, never
null
-
setAcceptedFileExtensions
Sets the accepted file extensions for uploads. Only files with matching extensions will be accepted. Extensions must start with a dot, e.g.".pdf",".txt".File extensions are used both as a client-side hint and for server-side validation.
If both MIME types and file extensions are configured, a file must match at least one of each (AND logic).
- Parameters:
extensions- the accepted file extensions, each starting with a dot; ornullto clear- Throws:
IllegalArgumentException- if any value is null, blank, or does not start with a dot
-
getAcceptedFileExtensions
Gets the list of accepted file extensions for upload.- Returns:
- a list of accepted file extensions, 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 Registration addFileRemovedListener(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 Registration addFileRejectedListener(ComponentEventListener<UploadManager.FileRejectedEvent> listener) Adds a listener forfile-rejectevents fired when a file cannot be added due to some constraints:setMaxFileSize, setMaxFiles, setAcceptedMimeTypes, setAcceptedFileExtensions- Parameters:
listener- the listener- Returns:
- a
Registrationfor removing the event listener
-
addAllFinishedListener
public Registration addAllFinishedListener(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
-