Class Upload

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.upload.Upload
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, Serializable

@Tag("vaadin-upload") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.4.5") @NpmPackage(value="@vaadin/upload",version="24.4.5") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/upload/src/vaadin-upload.js") public class Upload extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle
Upload is a component for uploading one or more files. It shows the upload progression and status of each file. Files can be uploaded using the Upload button or via drag and drop.
Author:
Vaadin Ltd.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance of Upload.
    Upload(Receiver receiver)
    Create a new instance of Upload with the given receiver.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    addAllFinishedListener(com.vaadin.flow.component.ComponentEventListener<AllFinishedEvent> listener)
    Add listener that is informed on all uploads finished.
    com.vaadin.flow.shared.Registration
    addFailedListener(com.vaadin.flow.component.ComponentEventListener<FailedEvent> listener)
    Add a succeeded listener that is informed on upload failure.
    com.vaadin.flow.shared.Registration
    addFileRejectedListener(com.vaadin.flow.component.ComponentEventListener<FileRejectedEvent> listener)
    Adds a listener for file-reject events fired when a file cannot be added due to some constrains: setMaxFileSize, setMaxFiles, setAcceptedFileTypes
    com.vaadin.flow.shared.Registration
    addFileRemovedListener(com.vaadin.flow.component.ComponentEventListener<FileRemovedEvent> listener)
    Adds a listener for events fired when a file is removed.
    com.vaadin.flow.shared.Registration
    addFinishedListener(com.vaadin.flow.component.ComponentEventListener<FinishedEvent> listener)
    Add a succeeded listener that is informed on upload finished.
    com.vaadin.flow.shared.Registration
    addProgressListener(com.vaadin.flow.component.ComponentEventListener<ProgressUpdateEvent> listener)
    Add a progress listener that is informed on upload progress.
    com.vaadin.flow.shared.Registration
    addStartedListener(com.vaadin.flow.component.ComponentEventListener<StartedEvent> listener)
    Add a succeeded listener that is informed on upload start.
    com.vaadin.flow.shared.Registration
    addSucceededListener(com.vaadin.flow.component.ComponentEventListener<SucceededEvent> listener)
    Add a succeeded listener that is informed on upload succeeded.
    void
    Clear the list of files being processed, or already uploaded.
    protected void
    fireUpdateProgress(long totalBytes, long contentLength)
    Deprecated.
    since 24.4.
    protected void
    fireUpdateProgress(long totalBytes, long contentLength, String fileName)
    Emit the progress event.
    Get the list of accepted file types for upload.
    com.vaadin.flow.component.Component
    Get the component set as the drop label.
    com.vaadin.flow.component.Component
    Get the component set as the drop label icon.
    Get the internationalization object previously set for this component.
    int
    Get the maximum number of files allowed for the user to select to upload.
    int
    Get the maximum allowed file size in the client-side, in bytes.
    Return the current receiver.
    com.vaadin.flow.component.Component
    Get the component set as the upload button for the upload.
    void
    Interrupt the upload currently being received.
    boolean
    Get the auto upload status.
    boolean
    Get whether file dropping is allowed or not.
    boolean
    Is upload in progress.
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    void
    setAcceptedFileTypes(String... acceptedFileTypes)
    Specify the types of files that the Upload web-component accepts.
    void
    setAutoUpload(boolean autoUpload)
    When false, it prevents uploads from triggering immediately upon adding file(s).
    void
    setDropAllowed(boolean dropAllowed)
    Define whether the element supports dropping files on it for uploading.
    void
    setDropLabel(com.vaadin.flow.component.Component label)
    Set the component to show as a message to the user to drop files in the upload component.
    void
    setDropLabelIcon(com.vaadin.flow.component.Component icon)
    Set the component to show as the drop label icon.
    void
    Set the internationalization properties for this component.
    void
    setMaxFiles(int maxFiles)
    Limit of files to upload, by default it is unlimited.
    void
    setMaxFileSize(int maxFileSize)
    Specify the maximum file size in bytes allowed to upload.
    void
    Set the receiver implementation that should be used for this upload component.
    void
    setUploadButton(com.vaadin.flow.component.Component button)
    Set the component as the actionable button inside the upload component, that opens the dialog for choosing the files to be upload.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasSize

    getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
  • Constructor Details

    • Upload

      public Upload()
      Create a new instance of Upload.

      The receiver must be set before performing an upload.

    • Upload

      public Upload(Receiver receiver)
      Create a new instance of Upload with the given receiver.
      Parameters:
      receiver - receiver that handles the upload
  • Method Details

    • addAllFinishedListener

      public com.vaadin.flow.shared.Registration addAllFinishedListener(com.vaadin.flow.component.ComponentEventListener<AllFinishedEvent> listener)
      Add listener that is informed on all uploads finished.
      Parameters:
      listener - all finished listener to add
      Returns:
      a Registration for removing the event listener
    • 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
    • getMaxFiles

      public int getMaxFiles()
      Get the maximum number of files allowed for the user to select to upload.
      Returns:
      the maximum number of files
    • setMaxFileSize

      public void setMaxFileSize(int 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
    • getMaxFileSize

      public int getMaxFileSize()
      Get the maximum allowed file size in the client-side, in bytes.
      Returns:
      the maximum file size in bytes
    • setAutoUpload

      public void setAutoUpload(boolean autoUpload)
      When false, it prevents uploads from triggering immediately upon adding file(s). The default is true.
      Parameters:
      autoUpload - true to allow uploads to start immediately after selecting files, false otherwise.
    • isAutoUpload

      public boolean isAutoUpload()
      Get the auto upload status.
      Returns:
      true if the upload of files should start immediately after they are selected, false otherwise.
    • setDropAllowed

      public void setDropAllowed(boolean dropAllowed)
      Define whether the element supports dropping files on it for uploading. By default it's enabled in desktop and disabled in touch devices because mobile devices do not support drag events in general. Setting it true means that drop is enabled even in touch-devices, and false disables drop in all devices.
      Parameters:
      dropAllowed - true to allow file dropping, false otherwise
    • isDropAllowed

      public boolean isDropAllowed()
      Get whether file dropping is allowed or not. By default it's enabled in desktop and disabled in touch devices because mobile devices do not support drag events in general.
      Returns:
      true if file dropping is allowed, false otherwise.
    • setAcceptedFileTypes

      public void setAcceptedFileTypes(String... acceptedFileTypes)
      Specify the types of files that the Upload web-component 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"

      File format restrictions are checked only on the client side (browser). They indicate the hints for users as to what file types to upload. Using this method won’t restrict the uploaded file’s format on the server side. If required, it’s the responsibility of the application developer to implement application-specific restrictions on the server side in one or more of the Upload component’s event listeners (e.g., in addSucceededListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.upload.SucceededEvent>)).

      Parameters:
      acceptedFileTypes - the allowed file types to be uploaded, or null to clear any restrictions
    • getAcceptedFileTypes

      public List<String> getAcceptedFileTypes()
      Get the list of accepted file types for upload.
      Returns:
      a list of allowed file types, never null.
    • setUploadButton

      public void setUploadButton(com.vaadin.flow.component.Component button)
      Set the component as the actionable button inside the upload component, that opens the dialog for choosing the files to be upload.
      Parameters:
      button - the component to be clicked by the user to open the dialog, or null to reset to the default button
    • getUploadButton

      public com.vaadin.flow.component.Component getUploadButton()
      Get the component set as the upload button for the upload.
      Returns:
      the actionable button, never null.
    • setDropLabel

      public void setDropLabel(com.vaadin.flow.component.Component label)
      Set the component to show as a message to the user to drop files in the upload component. Despite of the name, the label can be any component.
      Parameters:
      label - the label to show for the users when it's possible drop files, or null to reset to the default label
    • getDropLabel

      public com.vaadin.flow.component.Component getDropLabel()
      Get the component set as the drop label.
      Returns:
      the drop label component, never null.
    • setDropLabelIcon

      public void setDropLabelIcon(com.vaadin.flow.component.Component icon)
      Set the component to show as the drop label icon. The icon is visible when the user can drop files to this upload component. Despite of the name, the drop label icon can be any component.
      Parameters:
      icon - the label icon to show for the users when it's possible to drop files, or null to reset to the default icon
    • getDropLabelIcon

      public com.vaadin.flow.component.Component getDropLabelIcon()
      Get the component set as the drop label icon.
      Returns:
      the drop label icon component, never null.
    • interruptUpload

      public void interruptUpload()
      Interrupt the upload currently being received.

      The interruption will be done by the receiving thread so this method will return immediately and the actual interrupt will happen a bit later.

      Note! this will interrupt all uploads in multi-upload mode.

    • isUploading

      public boolean isUploading()
      Is upload in progress.
      Returns:
      true if receiving upload
    • fireUpdateProgress

      @Deprecated(since="24.4") protected void fireUpdateProgress(long totalBytes, long contentLength)
      Deprecated.
      Emit the progress event.
      Parameters:
      totalBytes - bytes received so far
      contentLength - actual size of the file being uploaded, if known
    • fireUpdateProgress

      protected void fireUpdateProgress(long totalBytes, long contentLength, String fileName)
      Emit the progress event.
      Parameters:
      totalBytes - bytes received so far
      contentLength - actual size of the file being uploaded, if known
      fileName - name of the file being uploaded
    • addProgressListener

      public com.vaadin.flow.shared.Registration addProgressListener(com.vaadin.flow.component.ComponentEventListener<ProgressUpdateEvent> listener)
      Add a progress listener that is informed on upload progress.
      Parameters:
      listener - progress listener to add
      Returns:
      registration for removal of listener
    • addFailedListener

      public com.vaadin.flow.shared.Registration addFailedListener(com.vaadin.flow.component.ComponentEventListener<FailedEvent> listener)
      Add a succeeded listener that is informed on upload failure.
      Parameters:
      listener - failed listener to add
      Returns:
      registration for removal of listener
    • addFinishedListener

      public com.vaadin.flow.shared.Registration addFinishedListener(com.vaadin.flow.component.ComponentEventListener<FinishedEvent> listener)
      Add a succeeded listener that is informed on upload finished.
      Parameters:
      listener - finished listener to add
      Returns:
      registration for removal of listener
    • addStartedListener

      public com.vaadin.flow.shared.Registration addStartedListener(com.vaadin.flow.component.ComponentEventListener<StartedEvent> listener)
      Add a succeeded listener that is informed on upload start.
      Parameters:
      listener - start listener to add
      Returns:
      registration for removal of listener
    • addSucceededListener

      public com.vaadin.flow.shared.Registration addSucceededListener(com.vaadin.flow.component.ComponentEventListener<SucceededEvent> listener)
      Add a succeeded listener that is informed on upload succeeded.
      Parameters:
      listener - succeeded listener to add
      Returns:
      registration for removal of listener
    • addFileRejectedListener

      public com.vaadin.flow.shared.Registration addFileRejectedListener(com.vaadin.flow.component.ComponentEventListener<FileRejectedEvent> listener)
      Adds a listener for file-reject events fired when a file cannot be added due to some constrains: setMaxFileSize, setMaxFiles, setAcceptedFileTypes
      Parameters:
      listener - the listener
      Returns:
      a Registration for removing the event listener
    • addFileRemovedListener

      public com.vaadin.flow.shared.Registration addFileRemovedListener(com.vaadin.flow.component.ComponentEventListener<FileRemovedEvent> listener)
      Adds a listener for events fired when a file is removed.
      Parameters:
      listener - the listener
      Returns:
      a Registration for removing the event listener
    • getReceiver

      public Receiver getReceiver()
      Return the current receiver.
      Returns:
      the StreamVariable.
    • setReceiver

      public void setReceiver(Receiver receiver)
      Set the receiver implementation that should be used for this upload component.

      Note! If the receiver doesn't implement MultiFileReceiver then the upload will be automatically set to only accept one file.

      Parameters:
      receiver - receiver to use for file reception
    • setI18n

      public void setI18n(UploadI18N i18n)
      Set the internationalization properties for this component.
      Parameters:
      i18n - the internationalized properties, not null
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • getI18n

      public UploadI18N getI18n()
      Get the internationalization object previously set for this component.

      Note: updating the object content that is gotten from this method will not update the language on the component if not set back using setI18n(UploadI18N)

      Returns:
      the object with the i18n properties. If the i18n properties weren't set, the object will return null.
    • clearFileList

      public void clearFileList()
      Clear the list of files being processed, or already uploaded.