Class Download
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeMethodDescriptionstatic RegistrationonClick(Component component, DownloadHandler handler) Saves the content of a handler as a file when the given component is clicked.static RegistrationonClick(Component component, DownloadHandler handler, DownloadTarget target) Delivers the content of a handler to the given target when the given component is clicked.static RegistrationSaves the content of a URL as a file when the given component is clicked.static RegistrationonClick(Component component, String url, DownloadTarget target) Delivers the content of a URL to the given target when the given component is clicked.static RegistrationLikeonClick(Component, String)but suggestsfileNameas the name of the saved file.
-
Method Details
-
onClick
Saves the content of a handler as a file when the given component is clicked. Same asonClick(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, notnullhandler- produces the file when the browser requests it, notnull- 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.setFileNameor the file name of aDownloadResponse.With
DownloadTarget.NEW_TAB, the browser shows the content only when it is served inline. The built-in handlers, such asDownloadHandler.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 anAnchor. A custom handler should callDownloadEvent.inlineitself, 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 thatallowDisabled()returns can no longer be switched to inline, so forDownloadTarget.NEW_TABcallinline()on a built-in handler first, for exampleDownloadHandler.forFile(file).inline().allowDisabled().- Parameters:
component- the component whose clicks deliver the content, notnullhandler- produces the content when the browser requests it, notnulltarget- where the browser puts the content, notnull- Returns:
- a registration for removing the behavior from the component; after removal the handler is no longer served
-
onClick
Saves the content of a URL as a file when the given component is clicked. Same asonClick(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, notnullurl- the URL to download from, notnull- Returns:
- a registration for removing the download from the component
-
onClick
Delivers the content of a URL to the given target when the given component is clicked. WithDownloadTarget.NEW_TABthe 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, notnullurl- the URL of the content, notnulltarget- where the browser puts the content, notnull- Returns:
- a registration for removing the behavior from the component
- Throws:
IllegalArgumentException- iftargetisDownloadTarget.NEW_TABandurlis ajavascript:URL
-
onClick
LikeonClick(Component, String)but suggestsfileNameas 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 aContent-Dispositionheader.- Parameters:
component- the component whose clicks start the download, notnullurl- the URL to download from, notnullfileName- the suggested file name, notnull- Returns:
- a registration for removing the download from the component
-