Class DownloadAction
java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.DownloadAction
- All Implemented Interfaces:
Serializable
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:
- A URL the application already knows — a server endpoint, an
external CDN, a
blob:URL minted on the client, anything addressable. UseDownloadAction(String)orDownloadAction(String, String)to also suggest a filename. - A
DownloadHandlerfor server-generated content — seeDownloadHandler.forFile(java.io.File),DownloadHandler.forClassResource(java.lang.Class<?>, java.lang.String),DownloadHandler.fromInputStream(com.vaadin.flow.server.streams.InputStreamDownloadCallback), or any lambda. UseDownloadAction(DownloadHandler). The action lazily registers a stream resource per trigger, scoped to its host element; the resource is unregistered when the host detaches and re-registered on re-attach, keeping the URL stable for the lifetime of the action. Removing the trigger unregisters the resource for good, so its URL stops serving the file. Filename comes from theDownloadHandleritself (its URL postfix and/orContent-Dispositionheader). - A value resolved from client state at fire time — use
DownloadAction(Action.Input)orDownloadAction(Action.Input, Action.Input)when the URL (or filename) is only known on the client, e.g. the current value of an input field, or ablob:URL the client just produced.
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionDownloadAction(Action.Input<String> url) Downloads from a URL resolved on the client at fire time.DownloadAction(Action.Input<String> url, Action.Input<String> filename) LikeDownloadAction(Action.Input)but also resolves the suggested filename on the client.DownloadAction(DownloadHandler handler) Downloads from aDownloadHandler.DownloadAction(String url) Downloads from a known URL with no filename hint.DownloadAction(String url, String filename) Downloads from a known URL and suggestsfilenameas the saved name. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsFunctionBuilds theJsFunctionthat runs this action when the surrounding trigger fires.Methods inherited from class com.vaadin.flow.component.trigger.internal.Action
warnIfNotVisible
-
Constructor Details
-
DownloadAction
Downloads from a known URL with no filename hint.- Parameters:
url- the URL to download from, notnull
-
DownloadAction
Downloads from a known URL and suggestsfilenameas the saved name. The hint is ignored by the browser for cross-origin URLs; provide aContent-Dispositionresponse header in that case.- Parameters:
url- the URL to download from, notnullfilename- the suggested filename, notnull
-
DownloadAction
Downloads from aDownloadHandler. 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.- Parameters:
handler- produces the response when the URL is fetched, notnull
-
DownloadAction
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 ablob:URL the client just minted.- Parameters:
url- input supplying the URL when the trigger fires, notnull
-
DownloadAction
LikeDownloadAction(Action.Input)but also resolves the suggested filename on the client.- Parameters:
url- input supplying the URL when the trigger fires, notnullfilename- input supplying the suggested filename when the trigger fires, notnull
-
-
Method Details
-
toJs
Description copied from class:ActionBuilds theJsFunctionthat runs this action when the surrounding trigger fires. The returned function takes one runtime argument namedevent(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'sJsFunctionas a capture and invoke it inside the body as$N(event).
-