How to start a file download in vaadin without button?

后端 未结 2 1869
我在风中等你
我在风中等你 2021-02-04 09:21

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?

2条回答
  •  离开以前
    2021-02-04 09:58

    Here is my work-around. It works like a charm for me. Hope it will help you.

    1. 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;
      }
      
    2. 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();");
        }
      });
      

提交回复
热议问题