Gunnar2
(Gunnar Schmid)
November 25, 2024, 12:21pm
1
Hi,
how can a file download be triggered asynchronously, i. e. not by direct user action (button click etc.)? In my use case the app receives a message from another application and then should initiate the download.
I tried with Page.open(), but it doesn’t seem to work.
Assuming, that your file is provided using a normal anchor, you can simply “click” it in the client using the element / js api from Java.
downloadAnchor.getElement().callJsFunction("click");
If you are inside some async thread, that is not the current request handling thread, you need to wrap that call inside an UI access.
// ... inside the async thread
downloadAnchor.getUI().ifPresent(ui -> ui.access(() -> downloadAnchor.getElement().callJsFunction("click")));
Also check this post, maybe it covers your use case, too.
Hi,
I’m trying to implement a button that triggers a file download. I’m not using an anchor because that would require me to pre-register the resource, but I want to avoid preloading the data.
Based on an older forum post (Download link or button - #8 by Marco36 ) I have something like this in my button click listener:
var inputStream = ...; // secret code for getting the input stream.
final StreamResource resource = new StreamResource("myfile.zip", () -> {
try {
return inputS…
knoobie
(Christian Knoop)
November 25, 2024, 1:15pm
4
Good luck. That’s going to fail without user interaction thanks to security consideration.
Gunnar2
(Gunnar Schmid)
November 25, 2024, 1:21pm
5
Hm. It did work in the old Vaadin 8 app, so I’d assume that it somehow should be possible in Flow 24, too.
Gunnar2
(Gunnar Schmid)
November 25, 2024, 3:09pm
6
Turns out that it works well with Page.open(…). The issue was that I used the sandbox attribute in the iframe where the app is embedded.