I know that it is really easy to create a FileDownloader
and call extend with a Button
. But how do I start a download without the Button
?
Here is my work-around. It works like a charm for me. Hope it will help you.
Create a button and hide it by Css (NOT by code: button.setInvisible(false))
final Button downloadInvisibleButton = new Button();
downloadInvisibleButton.setId("DownloadButtonId");
downloadInvisibleButton.addStyleName("InvisibleButton");
In your theme, add this rule to hide the downloadInvisibleButton
:
.InvisibleButton {
display: none;
}
When the user clicks on menuItem: extend the fileDownloader
to the downloadInvisibleButton
, then simulate the click on the downloadInvisibleButton
by JavaScript.
menuBar.addItem("Download", new MenuBar.Command() {
@Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
FileDownloader fileDownloader = new FileDownloader(...);
fileDownloader.extend(downloadInvisibleButton);
//Simulate the click on downloadInvisibleButton by JavaScript
Page.getCurrent().getJavaScript()
.execute("document.getElementById('DownloadButtonId').click();");
}
});