Class DownloadAction

java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.DownloadAction
All Implemented Interfaces:
Serializable

public class DownloadAction extends Action
Starts a file download in the browser when the bound trigger fires. The URL is loaded into a synthesised <a href download> that is clicked synchronously inside the trigger's handler — so the call happens inside the user gesture and is not subject to popup blockers.

Three ways to specify what to download:

The download attribute that suggests a filename is honoured only for same-origin URLs. For cross-origin downloads the server must send the filename in a Content-Disposition header itself.

No outcome callback: the browser does not notify when (or whether) the file is actually saved.


 // Static URL
 new ClickTrigger(button).triggers(
         new DownloadAction("/api/reports/2026-Q1.pdf", "report.pdf"));

 // Server-generated content
 new ClickTrigger(button)
         .triggers(new DownloadAction(DownloadHandler.forFile(reportFile)));

 // URL taken from a text field at fire time
 Action.Input<String> currentUrl = new PropertyInput<>(urlField, "value",
         String.class);
 new ClickTrigger(button).triggers(new DownloadAction(currentUrl));
 
For internal use only. May be renamed or removed in a future release.
See Also:
  • Constructor Details

    • DownloadAction

      public DownloadAction(String url)
      Downloads from a known URL with no filename hint.
      Parameters:
      url - the URL to download from, not null
    • DownloadAction

      public DownloadAction(String url, String filename)
      Downloads from a known URL and suggests filename as the saved name. The hint is ignored by the browser for cross-origin URLs; provide a Content-Disposition response header in that case.
      Parameters:
      url - the URL to download from, not null
      filename - the suggested filename, not null
    • DownloadAction

      public DownloadAction(DownloadHandler handler)
      Downloads from a DownloadHandler. The handler is registered as a stream resource scoped to the trigger host element the first time this action is wired to a trigger; re-attaching the host re-registers the resource with the same URL.
      Parameters:
      handler - produces the response when the URL is fetched, not null
    • DownloadAction

      public DownloadAction(Action.Input<String> url)
      Downloads from a URL resolved on the client at fire time. Use this when the URL is only known in the browser — for example, the current contents of an input field, or a blob: URL the client just minted.
      Parameters:
      url - input supplying the URL when the trigger fires, not null
    • DownloadAction

      public DownloadAction(Action.Input<String> url, Action.Input<String> filename)
      Like DownloadAction(Action.Input) but also resolves the suggested filename on the client.
      Parameters:
      url - input supplying the URL when the trigger fires, not null
      filename - input supplying the suggested filename when the trigger fires, not null
  • Method Details

    • toJs

      protected JsFunction toJs(Trigger trigger)
      Description copied from class: Action
      Builds the JsFunction that runs this action when the surrounding trigger fires. The returned function takes one runtime argument named event (declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.

      The body is one statement. To embed a value produced on the client, capture an Action.Input's JsFunction as a capture and invoke it inside the body as $N(event).

      Specified by:
      toJs in class Action
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the action's JS function, not null