Trigger download asynchonously

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.

Good luck. That’s going to fail without user interaction thanks to security consideration.

Hm. It did work in the old Vaadin 8 app, so I’d assume that it somehow should be possible in Flow 24, too.

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.