1. Domino UI
  2. Forms
  3. File upload
Top Upload box Drag drop upload Docs

Upload

Upload box

The UploadBox class represents an input field for uploading files. It extends the InputFormField class and provides the ability to select and display uploaded files.

Usage Example:

 
 // Create an UploadBox with a label
 UploadBox uploadBox = UploadBox.create("Choose Files:");

 // Set the accepted file types (e.g., images)
 uploadBox.setAccepts("image/*");

 // Enable multiple file selection
 uploadBox.setMultiple(true);

 // Add a change listener to handle file selection changes
 uploadBox.addChangeHandler(event -> {
     List  selectedFiles = event.getTarget().getValue();
     // Handle the selected files
 });
 

File upload

The `FileUpload` class provides a user interface for uploading files to a server. It allows users to select and upload one or multiple files, with options for customization and configuration.

Usage example:

 
 // Create a FileUpload component
 FileUpload fileUpload = FileUpload.create();

 // Set the maximum number of allowed uploads
 fileUpload.setMaxAllowedUploads(5);

 // Set a custom request sender for handling file uploads
 fileUpload.setRequestSender(myCustomRequestSender);

 // Enable auto-upload (files are uploaded automatically after selection)
 fileUpload.autoUpload();

 // Set the accepted file types
 fileUpload.accept("image/*,application/pdf");

 // Add an event handler for when a file is added
 fileUpload.onAddFile(fileItem -> {
     // Handle the added fileItem
 });

 // Set a custom decoration for the component
 fileUpload.setDecoration(myCustomDecorationElement);
 

See also :

File item

Represents an item in the file upload component that corresponds to a selected file for upload. It encapsulates the file, upload options, and various event handlers.

See also :

File preview

The IsFilePreview interface represents a file preview component for use in file uploads. Implementations of this interface define the behavior and appearance of file previews associated with individual files in a file upload component.

T

The type of the underlying component used to render the file preview.

Upload options

Examples

Upload box Upload file using a simple form field box.

Drag/Drop upload Browse or Drag/Drop to start uploading

API Docs: UploadBox

Constructors

public void UploadBox()
Constructs a new UploadBox instance with default settings.
public void UploadBox(String label)
Constructs a new UploadBox instance with a specified label.

label

The label for the UploadBox .

Static methods

public static UploadBox create()
Creates a new UploadBox instance with default settings.

Returns:

A new UploadBox instance.

public static UploadBox create(String label)
Creates a new UploadBox instance with a specified label.

label

The label for the UploadBox .



Returns:

A new UploadBox instance.

Public methods

public UploadBox setPostfix(String postfix)
Sets the postfix text for the UploadBox .

postfix

The text to be displayed as a postfix.



Returns:

This UploadBox instance.

public String getPostfix()
Gets the postfix text of the UploadBox .

Returns:

The postfix text of the UploadBox .

public UploadBox setPrefix(String prefix)
Sets the prefix text for the UploadBox .

prefix

The text to be displayed as a prefix.



Returns:

This UploadBox instance.

public String getPrefix()
Gets the prefix text of the UploadBox .

Returns:

The prefix text of the UploadBox .

public PrefixElement getPrefixElement()
Gets the prefix element of the UploadBox .

Returns:

The prefix element as a DivElement .

public UploadBox withPrefixElement()
Initializes and retrieves the prefix element of the UploadBox .

Returns:

This UploadBox instance.

public UploadBox withPostfixElement()
Initializes and retrieves the postfix element of the UploadBox .

Returns:

This UploadBox instance.

public String getName()
Gets the name attribute of the UploadBox .

Returns:

The name attribute.

public UploadBox setName(String name)
Sets the name attribute of the UploadBox .

name

The name attribute to set.



Returns:

This UploadBox instance.

public String getStringValue()
Gets the string representation of the selected files' names, separated by commas.

Returns:

The selected files' names as a string.

public String getType()
Gets the type of the input element, which is always "file" for the UploadBox .

Returns:

The input element type.

public List<File> getValue()
Gets the list of selected files in the UploadBox .

Returns:

A list of selected File objects.

public UploadBox setAccepts(String[] accepts)
Sets the accepted file types for the UploadBox .

accepts

The file types to accept (e.g., "image/*").



Returns:

This UploadBox instance.

public String getAccepts()
Gets the accepted file types for the UploadBox .

Returns:

The accepted file types.

public UploadBox setMultiple(boolean multiple)
Enables or disables multiple file selection for the UploadBox .

multiple

true to enable multiple file selection, false to disable it.



Returns:

This UploadBox instance.

public boolean getMultiple()
Checks if multiple file selection is enabled for the UploadBox .

Returns:

true if multiple file selection is enabled, false otherwise.

API Docs: FileUpload

Constructors

public void FileUpload()
Creates a new instance of the `FileUpload` component with default configurations.
public void FileUpload(FilePreviewFactory filePreviewFactory)
Creates a new instance of the `FileUpload` component with a custom file preview factory and the default file preview container.

filePreviewFactory

The file preview factory to use.

public void FileUpload(FilePreviewFactory filePreviewFactory, FilePreviewContainer<?, ?> filePreviewContainer)
Creates a new instance of the `FileUpload` component with a custom file preview factory and file preview container.

filePreviewFactory

The file preview factory to use.

filePreviewContainer

The file preview container to use.

public void FileUpload(FilePreviewFactory filePreviewFactory, FilePreviewContainer<?, ?> filePreviewContainer, IsElement<?> decoration)
Creates a new instance of the `FileUpload` component with a custom file preview factory, file preview container, and decoration element.

filePreviewFactory

The file preview factory to use.

filePreviewContainer

The file preview container to use.

decoration

The decoration element to use.

Static methods

public static FileUpload create()
Creates a new instance of the `FileUpload` component.

Returns:

A new `FileUpload` instance.

public static FileUpload create(FilePreviewFactory filePreviewFactory, FilePreviewContainer<?, ?> filePreviewContainer)
Creates a new instance of the `FileUpload` component with a custom file preview factory and file preview container.

filePreviewFactory

The file preview factory to use.

filePreviewContainer

The file preview container to use.



Returns:

A new `FileUpload` instance with the specified factory and container.

public static FileUpload create(FilePreviewContainer<?, ?> filePreviewContainer)
Creates a new instance of the `FileUpload` component with a default file preview factory and a custom file preview container.

filePreviewContainer

The file preview container to use.



Returns:

A new `FileUpload` instance with the default factory and the specified container.

public static FileUpload create(FilePreviewFactory filePreviewFactory)
Creates a new instance of the `FileUpload` component with a custom file preview factory and the default file preview container.

filePreviewFactory

The file preview factory to use.



Returns:

A new `FileUpload` instance with the specified factory and the default container.

public static FileUpload create(FilePreviewFactory filePreviewFactory, FilePreviewContainer<?, ?> filePreviewContainer, IsElement<?> decoration)
Creates a new instance of the `FileUpload` component with a custom file preview factory, file preview container, and decoration element.

filePreviewFactory

The file preview factory to use.

filePreviewContainer

The file preview container to use.

decoration

The decoration element to use.



Returns:

A new `FileUpload` instance with the specified factory, container, and decoration.

Public methods

public FileUpload setDecoration(IsElement<?> decoration)
Sets a custom decoration element for the `FileUpload` component using an IsElement instance. The decoration element is typically used to enhance or style the appearance of the component.

decoration

The decoration element provided as an IsElement instance.



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload setDecoration(Element decoration)
Sets a custom decoration element for the `FileUpload` component using a native Element . The decoration element is typically used to enhance or style the appearance of the component.

decoration

The decoration element provided as a native Element .



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload setMaxAllowedUploads(int maxAllowedUploads)
Sets the maximum number of allowed uploads.

maxAllowedUploads

The maximum number of allowed uploads.

public int getMaxAllowedUploads()
Gets the maximum number of allowed uploads.

Returns:

The maximum number of allowed uploads.

public FileUpload setRequestSender(UploadRequestSender requestSender)
Sets a custom request sender for handling file uploads.

requestSender

The custom request sender.



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload uploadFiles(List<File> files)
Uploads a list of files to the server.

files

The list of files to upload.

public FileUpload uploadAllFiles()
Uploads all added files to the server.
public HTMLDivElement element()
Check super implementation documentation.
public FileUpload setMultiUpload(boolean multiUpload)
Sets whether the component allows multiple file uploads.

multiUpload

Set to `true` to allow multiple file uploads, `false` otherwise.



Returns:

The current `FileUpload` instance for method chaining.

public boolean isMultiUpload()
Checks if the component allows multiple file uploads.

Returns:

`true` if multiple file uploads are allowed, `false` otherwise.

public FileUpload accept(String acceptedFiles)
Sets the accepted file types for file selection.

acceptedFiles

A comma-separated list of accepted file types (e.g., "image/*,application/pdf").



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload accept(Collection<String> acceptedFiles)
Sets the accepted file types for file selection from a collection of accepted file types.

acceptedFiles

A collection of accepted file types.



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload onAddFile(FileItemHandler fileItemHandler)
Adds a handler for when a file is added to the component.

fileItemHandler

The file item handler to add.



Returns:

The current `FileUpload` instance for method chaining.

public FileUpload autoUpload()
Enables auto-upload mode, where files are uploaded automatically after selection.

Returns:

The current `FileUpload` instance for method chaining.

public FileUpload manualUpload()
Enables manual-upload mode, where files are not automatically uploaded after selection.

Returns:

The current `FileUpload` instance for method chaining.

public FileUpload setAutoUpload(boolean autoUpload)
Sets the upload mode for the component.

autoUpload

Set to `true` for auto-upload mode, `false` for manual-upload mode.



Returns:

The current `FileUpload` instance for method chaining.

public InputElement getInputElement()
Gets the input element used for selecting files.

Returns:

The input element for file selection.

public DominoElement<HTMLElement> getFilesContainer()
Gets the container element that holds the added file items.

Returns:

The container element for added file items.

public List<FileItem> getAddedFileItems()
Gets the list of added file items.

Returns:

A list of added file items.

public FileUpload removeFileItems()
Removes all added file items from the component.

Returns:

The current `FileUpload` instance for method chaining.

public List<FileItemHandler> getOnAddFileHandlers()
Gets a list of file item handlers that are executed when a file is added to the component.

Returns:

A list of file item handlers.

public boolean isAutoUpload()
Checks if the component is in auto-upload mode.

Returns:

`true` if the component is in auto-upload mode, `false` otherwise.

public String getName()
Retrieves the name attribute of the hidden file input element associated with this `FileUpload` component. The name attribute is used when submitting the uploaded file(s) as part of a form.

Returns:

The name attribute of the hidden file input element.

public FileUpload setName(String name)
Sets the name attribute for the hidden file input element associated with this `FileUpload` component. The name attribute is used when submitting the uploaded file(s) as part of a form.

name

The name to be set as the name attribute.



Returns:

The current `FileUpload` instance for method chaining.

public Optional<DropEffect> getDropEffect()
Gets the current drop effect for file drag-and-drop operations.

Returns:

An optional drop effect, or `null` if not set.

public FileUpload setDropEffect(DropEffect dropEffect)
Sets the drop effect for file drag-and-drop operations.

dropEffect

The drop effect to set.



Returns:

The current `FileUpload` instance for method chaining.

public boolean isShowPreview()


Returns:

true if uploaded files will show a preview in the preview container

public FileUpload setShowPreview(boolean showPreview)
When set to true, uploaded files will show a preview in the preview container, otherwise they wont

showPreview

boolean.



Returns:

same component instance

API Docs: FileItem

Constructors

public void FileItem(File file, UploadOptions options, FilePreviewFactory previewFactory, FileUpload fileUpload)
Initializes a new FileItem with the given file, options, file preview factory, and file upload.

file

The file to be associated with this item.

options

The upload options for this file item.

previewFactory

The factory for creating the file preview.

fileUpload

The parent file upload component.

Static methods

public static FileItem create(File file, UploadOptions options, FilePreviewFactory previewFactory, FileUpload fileUpload)
Creates a new FileItem instance for the given file, options, file preview factory, and file upload.

file

The file to be associated with this item.

options

The upload options for this file item.

previewFactory

The factory for creating the file preview.

fileUpload

The parent file upload component.



Returns:

A new FileItem instance.

public static FileItem create(File file, FilePreviewFactory previewFactory, FileUpload fileUpload)
Creates a new FileItem instance for the given file, using the default upload options and the provided file preview factory and file upload.

file

The file to be associated with this item.

previewFactory

The factory for creating the file preview.

fileUpload

The parent file upload component.



Returns:

A new FileItem instance.

Public methods

public boolean isImage()
Checks if the file associated with this item is an image.

Returns:

true if the file is an image, false otherwise.

public File getFile()
Retrieves the file associated with this item.

Returns:

The associated file.

public String readableFileSize()
Converts the file size into a human-readable format.

Returns:

A human-readable string representing the file size.

public FileItem addRemoveHandler(RemoveFileHandler removeHandler)
Adds a handler for the removal of this file item.

removeHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem addErrorHandler(ErrorHandler errorHandler)
Adds a handler for upload errors.

errorHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem addProgressHandler(ProgressHandler progressHandler)
Adds a handler for tracking upload progress.

progressHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem addBeforeUploadHandler(BeforeUploadHandler beforeUploadHandler)
Adds a handler to be executed before the file upload.

beforeUploadHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem addSuccessUploadHandler(SuccessUploadHandler successUploadHandler)
Adds a handler for successful upload completion.

successUploadHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem addCancelHandler(CancelHandler cancelHandler)
Adds a handler for cancelling the file upload.

cancelHandler

The handler to be added.



Returns:

This FileItem instance to allow method chaining.

public FileItem withOptions(ChildHandler<FileItem, UploadOptions> handler)
Applies a child handler to set upload options for this file item.

handler

The child handler for configuring upload options.



Returns:

This FileItem instance to allow method chaining.

public UploadOptions getOptions()
Retrieves the upload options associated with this file item.

Returns:

The upload options.

public boolean isExceedsMaxFile()
Checks whether the file size exceeds the maximum allowed size.

Returns:

true if the file size exceeds the maximum allowed size, otherwise false .

public FileItem remove()
Removes the file item from the DOM and marks it as removed.

Returns:

This FileItem instance for method chaining.

public FileItem setUrl(String url)
Sets the URL to which the file will be uploaded.

url

The URL to set for the file upload.



Returns:

This FileItem instance for method chaining.

public String getFileName()
Gets the name of the file.

Returns:

The name of the file.

public void setFileName(String fileName)
Sets the name of the file.

fileName

The name to set for the file.

public List<RemoveFileHandler> getRemoveHandlers()
Gets a list of handlers for file removal events.

Returns:

A list of RemoveFileHandler instances.

public List<ErrorHandler> getErrorHandlers()
Gets a list of handlers for file error events.

Returns:

A list of ErrorHandler instances.

public List<ProgressHandler> getProgressHandlers()
Gets a list of handlers for file progress events.

Returns:

A list of ProgressHandler instances.

public List<BeforeUploadHandler> getBeforeUploadHandlers()
Gets a list of handlers for before upload events.

Returns:

A list of BeforeUploadHandler instances.

public List<SuccessUploadHandler> getSuccessUploadHandlers()
Gets a list of handlers for successful file upload events.

Returns:

A list of SuccessUploadHandler instances.

public FileItem cancel()
Cancels the file upload by aborting the associated request if it exists.

Returns:

This FileItem instance for method chaining.

public List<CancelHandler> getCancelHandlers()
Gets a list of handlers for cancel events during file upload.

Returns:

A list of CancelHandler instances.

public boolean isCanceled()
Checks if the file upload has been canceled.

Returns:

true if the file upload has been canceled, otherwise false .

public boolean isRemoved()
Checks if the file item has been removed from the DOM.

Returns:

true if the file item has been removed, otherwise false .

public boolean isUploaded()
Checks if the file has been successfully uploaded.

Returns:

true if the file has been successfully uploaded, otherwise false .

public void validateSize()
Validates the size of the file and displays an error message if it exceeds the maximum allowed size.

See also :

public FileUpload getFileUpload()
Gets the parent FileUpload component to which this file item belongs.

Returns:

The parent FileUpload instance.

API Docs: IsFilePreview

Public methods

public abstract void onUploadFailed(String error)
Handles the event when a file upload fails.

error

The error message associated with the failed upload.

public abstract void onUploadSuccess()
Handles the event when a file upload is successfully completed.
public abstract void onUploadCompleted()
Handles the event when a file upload is completed (whether successful or not).
public abstract void onUploadCanceled()
Handles the event when a file upload is canceled.
public abstract void onUploadProgress(double progress)
Handles the event when a file upload progress is updated.

progress

The progress value (usually a percentage) of the file upload.

public abstract void onReset()
Handles the event when the file preview component is reset to its initial state.
public abstract void onUploadStarted()
Handles the event when a file upload is started.
public abstract IsFilePreview<T> withComponent(ChildHandler<IsFilePreview<T>, T> handler)
Configures the file preview with a child component using the provided handler.

handler

The handler to configure the file preview with a child component.



Returns:

The file preview instance with the configured child component.

API Docs: UploadOptions

Constructors

public void UploadOptions()
Constructs a new UploadOptions instance with default settings.

Public methods

public UploadOptions setXMLHttpRequest(XMLHttpRequest xmlHttpRequest)
Sets the custom XML HTTP request object to be used for file uploads.

xmlHttpRequest

The custom XMLHttpRequest object.



Returns:

This UploadOptions instance for method chaining.

public Optional<XMLHttpRequest> getXmlHttpRequest()
Retrieves the custom XML HTTP request object configured for file uploads.

Returns:

An optional XMLHttpRequest object, or empty if not set.

public double getMaxFileSize()
Retrieves the maximum allowed file size for uploads.

Returns:

The maximum file size in bytes.

public UploadOptions setMaxFileSize(double maxFileSize)
Sets the maximum allowed file size for uploads.

maxFileSize

The maximum file size in bytes.



Returns:

This UploadOptions instance for method chaining.

public String getUrl()
Retrieves the target URL for file uploads.

Returns:

The target URL for uploads.

public UploadOptions setUrl(String url)
Sets the target URL for file uploads.

url

The target URL for uploads.



Returns:

This UploadOptions instance for method chaining.

public Supplier<List<Integer>> getSuccessCodesProvider()
Retrieves the supplier for the list of success status codes for uploads.

Returns:

A Supplier providing a list of success status codes.

public UploadOptions setSuccessCodesProvider(Supplier<List<Integer>> successCodesProvider)
Sets the supplier for the list of success status codes for uploads.

successCodesProvider

A Supplier providing a list of success status codes.



Returns:

This UploadOptions instance for method chaining.

We are a group of passionate people who love what we do

Donate & Support Us