fileStorageUploadField
fileStorageUploadField allows users to upload a file to the Jmix file storage.
XML Element |
|
|---|---|
Java Class |
|
Basics
The component can contain a label, a link to the uploaded file, and an upload button. When the upload button is clicked, a standard file picker dialog pops up, allowing the user to select a file.
fileStorageUploadField can work both without being bound to an entity attribute and with data binding.
Below we will show an example of using the component without data binding. You can use it to handle the uploaded file directly without associating it with any Jmix entity.
<fileStorageUploadField id="fileRefField"
acceptedFileTypes=".txt, .csv"
fileStoragePutMode="MANUAL"
fileNameVisible="true"/>
Then you can handle the uploaded file directly within the FileUploadSucceededEvent.
|
To store a file in an entity attribute as a byte array instead of To upload multiple files simultaneously, you should use the upload component instead of |
Internally, fileStorageUploadField uses Vaadin’s UploadHandler API. By default, the uploaded file is immediately stored in the default FileStorage. To control the saving of the file programmatically, use the fileStoragePutMode attribute.
|
Data-aware fileStorageUploadField
fileStorageUploadField allows users to upload a file to the file storage and link it to an entity attribute as a FileRef object.
In the example below, the picture attribute of the User entity has the FileRef type.
@Column(name = "PICTURE", length = 1024)
private FileRef picture;
<data>
<instance id="documentDc" class="io.jmix.uisamples.entity.Document">
</instance>
</data>
<layout>
<fileStorageUploadField id="fileStorageUploadField" label="Select File"
dataContainer="documentDc" property="content"
fileNameVisible="false"
dropAllowed="true"
maxFileSize="51200"
fileStoragePutMode="MANUAL"
/>
</layout>
@ViewComponent
private FileStorageUploadField fileStorageUploadField;
@Autowired
private TemporaryStorage temporaryStorage;
@Autowired
private Notifications notifications;
// This handler is needed only when fileStoragePutMode="MANUAL".
// In AUTO mode the field automatically moves the uploaded file from temporary storage
// to the FileStorage and sets the returned FileRef to the entity attribute.
@Subscribe("fileStorageUploadField")
public void onFileStorageUploadFieldFileUploadSucceeded(
final FileUploadSucceededEvent<FileStorageUploadField, TemporaryStorage.FileInfo> event) {
UUID fileId = event.getData().getId();
File file = temporaryStorage.getFile(fileId);
if (file != null) {
FileRef fileRef = new FileRef("tempStorage", file.getAbsolutePath(), file.getName());
fileStorageUploadField.setValue(fileRef);
notifications.create("Your file %s has been uploaded successfully.".formatted(event.getFileName()))
.withThemeVariant(NotificationVariant.LUMO_PRIMARY)
.show();
// Remove the uploaded file.
// In a real-world application you would move the file to FileStorage here using
// the temporaryStorage.putFileIntoStorage() method.
temporaryStorage.deleteFile(fileId);
}
}
fileStorageUploadField has a link to the container specified in the dataContainer attribute; the property attribute contains the name of the entity attribute that is displayed in fileStorageUploadField.
Attributes
The following attributes are specific to fileStorageUploadField:
| Name | Description | Default |
|---|---|---|
Specifies the types of files that the server accepts. |
— |
|
Sets the |
— |
|
Controls whether the field displays a clear button. |
|
|
Sets the status shown while the upload dialog connects. |
— |
|
Sets whether the component supports dropping files for uploading. |
— |
|
The |
— |
|
Sets the text shown when no file has been selected. |
— |
|
Sets the name of FileStorage where the upload file will be placed. |
— |
|
Sets mode which determines when a file will be put into FileStorage. |
— |
|
Sets the message shown when a file exceeds the maximum size. |
— |
|
Sets the message shown when a file type is not accepted. |
— |
|
Specifies the maximum file size in bytes allowed for upload. |
— |
|
Sets the status shown while the uploaded file is processed. |
— |
|
Sets the text used to display the remaining upload time. |
— |
|
Sets the text shown when the remaining upload time cannot be calculated. |
— |
|
Sets the text of the upload dialog’s cancel button. |
— |
|
Sets the title of the upload dialog. |
— |
|
Sets an icon to the upload button. |
— |
|
Sets the text shown on the upload button. |
— |
The following shared attributes are supported by fileStorageUploadField:
id - alignSelf - classNames - colspan - css - dataContainer - enabled - errorMessage - height - helperText - label - maxHeight - maxWidth - minHeight - minWidth - property - readOnly - required - requiredMessage - visible - width
Handlers
The following handlers are specific to fileStorageUploadField:
| Name | Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Validates the component value. |
The following shared handlers are supported by fileStorageUploadField:
Elements
A fileStorageUploadField can include tooltip and uploadIcon as its nested elements.