Lazy Download Button
Lazy Download Button
This component allows you to display a normal Vaadin button to download content from your application without blocking the UI while preparing the content. This allows you to load huge files from your backend and let the browser download it when it's ready.
Please also have a look at the examples or the demo source code, if you want to see how to integrate the LDB.
Requires PUSH
Since the content preparation is done in a separate thread, this component needs Push to be activated. The component itself does not check for active Push.
Please lookup the official documentation of Vaadin on how to activate Push: https://vaadin.com/docs/flow/advanced/tutorial-push-configuration.html
Supported Vaadin versions
Please note, that I only support the latest Vaadin major version regarding bugs or feature requests. You still may use this addon for Vaadin 14+, but on your own risk. If necessary for any reason, I will update the minimal required Vaadin version.
Sample code
// Create a new instance - it needs at least a caption (or icon) and a // callback to create the content on click time. // // Please be aware, that the callback is called OUTSIDE of the current UI. // You need to use UI.access() if you want to access the UI. LazyDownloadButton button = new LazyDownloadButton("Download", () -> { try { // Provide the file content as an input stream. In a WAR project, this could be for instance return Files.newInputStream(Paths.get(getClass().getClassLoader().getResource("some-file").toURI())) } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } }); add(button);
// Provide a callback to create the file name on click time LazyDownloadButton button = new LazyDownloadButton("Download", () -> "my_file.txt", () -> // ... create the input stream here ); add(button);
// LDB extends Vaadin Button, so you may use a simple click listener LazyDownloadButton button = new LazyDownloadButton(...); button.setDisableOnClick(true); button.addClickListener(event -> { // show some feedback to the user, that the download is prepared in the background event.getSource().setText("Preparing download..."); }); // LDB also provides a "download start" listener - this listener is fired, when the client // side starts the download. Please be aware to NOT remove the button here or its content, otherwise // the download can fail. button.addDownloadStartsListener(event -> { // restore normal state, so that the user can download the content again LazyDownloadButton button = event.getSource(); button.setText("Download"); button.setEnabled(true); });
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
- Released
- 2019-11-14
- Maturity
- BETA
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 14+
- Browser
- Browser Independent
Vaadin Add-on Directory
Find open-source widgets, add-ons, themes, and integrations for your Vaadin application.