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.
Since:
25.2
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 each trigger; re-attaching the host re-registers the resource with the same URL. The content is served as an attachment, unless the handler calls DownloadEvent.inline() or DownloadEvent.inline(String) itself.
      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 two runtime arguments: event, the client event that fired the trigger, and context, the trigger context — an object describing what the trigger fired for, or undefined for triggers that have no such notion. A SinkTrigger rendered into a client-side template, for example, supplies {args}, the arguments the template called the action with, which is what lets one action serve every element a renderer draws.

      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, context) — always forwarding both arguments, so inputs that read the context work in every action.

      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