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
Flow Viritin - Vaadin Add-on Directory
The commons library for Vaadin 10+. Uploads, downloads, Geolocation, forms, fieds, fluent API...GitHub
Online Demo
Flow Viritin version 0.1.6
ByteArrayUploadField, UploadFileHandler, DynamicFileDownloader
Flow Viritin version 0.1.7
Introduced BorderLayout
Flow Viritin version 0.2.3
NewFeaturesAndBugFixes
Flow Viritin version 0.2.4
NewFeaturesAndBugFixes
Flow Viritin version 0.2.8
See github timeline for changes
Flow Viritin version 0.2.10
Fixed several issues that were broken with V14. Updated dependencies.
Flow Viritin version 0.2.9
Added DisclosurePanel that was forgotten to commit to last release.
Flow Viritin version 0.3.0
Tree component added
Flow Viritin version 0.3.1
Updates to the Tree component
Flow Viritin version 0.5.0
Version for non-LTS branch, mainly V19+
Flow Viritin version 0.5.1
Added simple "application framework" bulk UIs.
Flow Viritin version 0.5.2
Added missing css file for app framework
Flow Viritin version 0.7.0
Lot of enhancements and new features
* first draft of ElementCollecitonField (for Flow). Consider this still WIP
* Greatly improved ByteArrayUploadField
* Ton of other small improvements
Flow Viritin version 0.9.0
Some bugfixes and release via Maven Central
New Maven coordinates!!
in.virit:viritin:0.9.0
Flow Viritin version 1.2.0
Added WebStorage helper for localStorage/sessionStorage handling.
Flow Viritin version 1.3.0
Sane defaults for date pickers via locale
Flow Viritin version 1.5.0
Added Geolocation API support
Flow Viritin version 1.6.0
Added CustomLayout component
Flow Viritin version 1.6.1
bugix in dynamicfiledownload
Flow Viritin version 2.1.0
Improvements to work better with the "dev-bundle" in Vaadin 24+. Technically so that custom bundle is no more needed when using Viritin -> front-end build don't become any slower when dropping in this add-on.
Flow Viritin version 2.5.0
Completely renewed UploadFileHandler component
* No extra thread created for each upload. For apps having a lot of large uploads concurrently, this might save some resources.
* Streaming now properly works in Spring Boot apps as well (doesn't use multipart request at all)
* Not extending from com.vaadin.flow.component.upload.Upload anymore, some API might be missing, please file issus for things you are missing
Flow Viritin version 2.5.1
Completely renewed UploadFileHandler component in 2.5.x series:
* The good old API that passes the [input stream for you to handle](https://vaadin.com/blog/uploads-and-downloads-inputs-and-outputs).
* No extra thread created for each upload. For apps having a lot of large uploads concurrently, this might save some resources.
* Streaming now properly works in Spring Boot apps as well (doesn't use multipart request at all). With previous version and with plain Upload you only get to access the stream once it is uploaded completely.
* The clients side opens maximum 1 server connection by default when allowing multiple files to be uploaded. This way the network/server/http2 connection don't get choked if uploading a large number of files at once.
* Not extending from com.vaadin.flow.component.upload.Upload anymore, some API might be missing, please file issus for things you are missing
Flow Viritin version 2.13.0
Uses markdown-it on the browser to render markdown content by default. The main motivation was to reduce transitive dependencies on the Java side. Most users should not see a difference, but if you manually add `com.vladsch.flexmark:flexmark` dependency to your project, the "old behaviour" is back. RichText and MarkdownMessage contain hooks to customize the behaviour if needed.
Pros/cons:
* Less JVM dependencies
* Less JVM resources consumed
* More JS "dependencies"
* More browser resources consumed
* JS is loaded dynamically (to avoid requirement for custom front-end bundle) -> might cause a tiny delay for the first markdown to appear
* Tables ("non-standard markdown") supported out of the box
* Inline html not supported out of the box (default in markdown-it, doesn't sanitize the content)