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);
}));
The download starts in the browser's own click handler, without waiting for a
server round trip. 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 file, 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. 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 the file.
- Since:
- 25.4
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Component & ClickNotifier<?>>
RegistrationonClick(T component, DownloadHandler handler) Starts a download when the given component is clicked.static <T extends Component & ClickNotifier<?>>
RegistrationStarts a download when the given component is clicked.static <T extends Component & ClickNotifier<?>>
RegistrationLikeonClick(Component, String)but suggestsfileNameas the name of the saved file.
-
Method Details
-
onClick
public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, DownloadHandler handler) Starts a download when the given component is clicked. The version that takes a handler is for content produced on the server; the versions that take a URL are for content the browser can already address.The handler runs only when the browser requests the file, once per click. The file name and response headers come from the handler, for example from
DownloadEvent.setFileNameor the file name of aDownloadResponse.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 file; pass
handler.allowDisabled()to serve the file in that case.- Type Parameters:
T- the component type, must implementClickNotifier- 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 <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url) Starts a download when the given component is clicked. The version that takes a URL is for content the browser can already address, such as a static file or a REST endpoint; the version that takes a handler is for content produced on the server.- Type Parameters:
T- the component type, must implementClickNotifier- 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
public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url, String fileName) 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.- Type Parameters:
T- the component type, must implementClickNotifier- 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
-