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 delivering content to the browser from a click on a button or any other clickable component, either saved as a file or opened in a new browser tab:

 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);
 }));

 Button view = new Button("View invoice");
 Download.onClick(view, invoiceHandler, DownloadTarget.NEW_TAB);
 
The download starts, or the tab opens, in the browser's own click handler, without waiting for a server round trip, so popup blockers let a new tab through. Opening a tab from a server-side click listener, for example with Page.open, happens only after the round trip and may be blocked. 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 content, 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, or shows the error in the new tab. 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 or looked at the content.

Since:
25.4
See Also:
  • Method Details

    • onClick

      public static Registration onClick(Component component, DownloadHandler handler)
      Saves the content of a handler as a file when the given component is clicked. Same as onClick(component, handler, DownloadTarget.SAVE_AS_FILE). The versions that take a handler are for content produced on the server; the versions that take a URL are for content the browser can already address.
      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 Registration onClick(Component component, DownloadHandler handler, DownloadTarget target)
      Delivers the content of a handler to the given target when the given component is clicked. The version without a target saves the content as a file.

      The handler runs only when the browser requests the content, 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.

      With DownloadTarget.NEW_TAB, the browser shows the content only when it is served inline. The built-in handlers, such as DownloadHandler.forFile(java.io.File), are switched to serve the content inline. This changes the given handler instance for good, so it also serves inline wherever else it is used; do not reuse it where the content should be downloaded, such as in an Anchor. A custom handler should call DownloadEvent.inline itself, since content served as an attachment is saved instead of shown.

      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 content; pass handler.allowDisabled() to serve the content in that case. The handler that allowDisabled() returns can no longer be switched to inline, so for DownloadTarget.NEW_TAB call inline() on a built-in handler first, for example DownloadHandler.forFile(file).inline().allowDisabled().

      Parameters:
      component - the component whose clicks deliver the content, not null
      handler - produces the content when the browser requests it, not null
      target - where the browser puts the content, not null
      Returns:
      a registration for removing the behavior from the component; after removal the handler is no longer served
    • onClick

      public static Registration onClick(Component component, String url)
      Saves the content of a URL as a file when the given component is clicked. Same as onClick(component, url, DownloadTarget.SAVE_AS_FILE). The versions that take a URL are for content the browser can already address, such as a static file or a REST endpoint; the versions that take a handler are for content produced on the server.
      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 Registration onClick(Component component, String url, DownloadTarget target)
      Delivers the content of a URL to the given target when the given component is clicked. With DownloadTarget.NEW_TAB the URL can also be a route of the application or an external page. The version without a target saves the content as a file.
      Parameters:
      component - the component whose clicks deliver the content, not null
      url - the URL of the content, not null
      target - where the browser puts the content, not null
      Returns:
      a registration for removing the behavior from the component
      Throws:
      IllegalArgumentException - if target is DownloadTarget.NEW_TAB and url is a javascript: URL
    • onClick

      public static Registration onClick(Component 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.
      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