Class Download

java.lang.Object
com.vaadin.flow.component.download.Download
All Implemented Interfaces:
Serializable

public final class Download extends Object implements Serializable
Entry point for starting file downloads from a click on a button or any other clickable component:

 Button export = new Button("Export");
 Download.onClick(export, DownloadHandler.fromInputStream(event -> {
     byte[] report = reportService.createReport();
     return new DownloadResponse(new ByteArrayInputStream(report),
             "report.pdf", "application/pdf", report.length);
 }));
 
The download starts in the browser's own click handler, without waiting for a server round trip. Unlike an Anchor wrapping a button, the component stays a real button: it keeps its look, keyboard handling and click listeners.

The content of a DownloadHandler is created lazily: the handler runs only when the browser requests the file, so the file name, content type and other response headers can be decided at that point. If the handler throws or returns an error response, the browser reports a failed download instead of saving an empty file. Use the transfer callbacks of the handler (for example whenComplete) to react to success or failure on the server. The browser does not report whether the user actually saved the file.

Since:
25.4
See Also:
  • Method Details

    • onClick

      public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, DownloadHandler handler)
      Starts a download when the given component is clicked. The version that takes a handler is for content produced on the server; the versions that take a URL are for content the browser can already address.

      The handler runs only when the browser requests the file, once per click. The file name and response headers come from the handler, for example from DownloadEvent.setFileName or the file name of a DownloadResponse.

      The handler is registered for the clicked component and follows its lifecycle: it is only served while the component is attached, visible and enabled. A button that disables itself on click can be disabled before the browser requests the file; pass handler.allowDisabled() to serve the file in that case.

      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks start the download, not null
      handler - produces the file when the browser requests it, not null
      Returns:
      a registration for removing the download from the component; after removal the handler is no longer served
    • onClick

      public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url)
      Starts a download when the given component is clicked. The version that takes a URL is for content the browser can already address, such as a static file or a REST endpoint; the version that takes a handler is for content produced on the server.
      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks start the download, not null
      url - the URL to download from, not null
      Returns:
      a registration for removing the download from the component
    • onClick

      public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url, String fileName)
      Like onClick(Component, String) but suggests fileName as the name of the saved file. Browsers use the suggestion only for URLs of the same origin; for other URLs the server has to send the name in a Content-Disposition header.
      Type Parameters:
      T - the component type, must implement ClickNotifier
      Parameters:
      component - the component whose clicks start the download, not null
      url - the URL to download from, not null
      fileName - the suggested file name, not null
      Returns:
      a registration for removing the download from the component