One approach is to get the file as resource and set it to Anchor
. Then you can have a Button
which programmatically clicks the Anchor
.
Anchor anchor = new Anchor();
anchor.setText("Download");
anchor.getElement().getStyle().set("display", "none");
anchor.getElement().setAttribute("download", true);
anchor.setHref(resource);
Button button = new Button("Download");
button.addClickListener(event -> {
anchor.getElement().callJsFunction("click");
});
add(anchor, button);
There is also an add-on in Vaadin's Directory that packages the above process.
Note, if you want to have the Download button in the Dialog, make sure that Anchor is not in the Dialog, see another question.
You can find also more complete download example here.