【问题标题】:How to download file on button click in grid vaadin如何在网格 vaadin 中单击按钮下载文件
【发布时间】:2016-04-21 14:50:46
【问题描述】:

我正在尝试在单击按钮时下载文件,并且按钮出现在网格列 vaadin 中。我怎样才能做到这一点

Button tempDownloadBtn = new Button();
tempDownloadBtn.setId("tempdownloadbtn");
Grid tableList = new Grid();
tableList.addColumn("title", String.class).setHeaderCaption("TITLE").setSortable(false);
tableList.addColumn("creationDate", String.class).setHeaderCaption("CREATION DATE").setSortable(false);
tableList.addColumn("status", String.class).setHeaderCaption("STATUS").setSortable(false).setWidth(120).setRenderer(new HtmlRenderer());
tableList.addColumn("description", String.class).setHeaderCaption("DESCRIPTION").setSortable(false);
tableList.addColumn("transactionType", String.class).setHeaderCaption("TRANSACTION TYPE").setSortable(false);
tableList.addColumn("batchId", String.class).setSortable(false).setHidden(true);
tableList.addColumn("action", String.class).setHeaderCaption("ACTION").setSortable(false).setRenderer(new ButtonRenderer(e -> {
    Indexed indexed = tableList.getContainerDataSource();
    String action = indexed.getContainerProperty(e.getItemId(), "action").getValue().toString();
    if (action.equalsIgnoreCase("Download Passcode"))
    {
        String batchId = Encrypter.decodeString(indexed.getContainerProperty(e.getItemId(), "batchId").getValue().toString());
        DownloadVoucherDetailsResponseM downloadFileResponseM = new CommonB().dowloadVoucherDetails(batchId);
        if (downloadFileResponseM.getResponseCode().equals("0000"))
        {
            System.out.println("DOWNLOAD_FILE_PATH::: " + downloadFileResponseM.getFilePath());
            File downloadFile = new File(downloadFileResponseM.getFilePath());

            FileDownloader fileDownloader = new FileDownloader(new FileResource(downloadFile));
            fileDownloader.extend(tempDownloadBtn);

            Page.getCurrent().getJavaScript().execute("document.getElementById('tempdownloadbtn').click();");
        }
    }
}));

【问题讨论】:

  • 我认为不可能将 FileDownloader 与 Grid 和 ButtonRenderer 一起使用,因为您手头的每一行都没有 Button 组件。 1.) 尝试 Vaadin Table,您可以在其中为每一行生成按钮并使用 FIleDownloader。或者 2.) 在使用 Grid 并单击 ButtonRenderer 的事件处理程序时寻找另一种下载方式。
  • 您找到解决方案了吗?请分享。
  • 没有。在谷歌上进行大量搜索后,我没有找到任何解决方案,然后我决定继续前进。
  • 偶然发现了一个 Vaadin 插件,这可能会有所帮助:GridFileDownloader
  • Vaadin 8.1 应该允许在网格中包含组件,但更好的设计可能是打开窗口作为项目选择的结果,然后在弹出窗口中实现下载按钮。我会试试这个,因为对我来说 Table 不是一个选项,因为它实际上(几乎)被弃用了。

标签: vaadin vaadin7


【解决方案1】:

试试这个。 按钮下载按钮 = 新按钮(); downloadButton.setIcon(FontAwesome.DOWNLOAD);

File file = new File("D:\\..\\sample.txt");

StreamResource.StreamSource source = new StreamResource.StreamSource() {

    /**
     * 
     */
    private static final long serialVersionUID = 4367295093982409180L;

    public InputStream getStream() {
        InputStream in = null;
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            /* e.printStackTrace(); */
        }
        return in;

    }
};
StreamResource resource = new StreamResource(source, file.getName());
resource.setCacheTime(1);

FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(downloadButton);

【讨论】:

  • 我认为这是实现下载的正确方法,但不能简单地将按钮和网格集成到 vaadin 7.x 和 8.0 中。至少我不知道如何触发按钮的单击事件作为网格项目选择的结果。
猜你喜欢
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
相关资源
最近更新 更多