如何在网格vaadin中单击按钮上下载文件

时间:2016-04-21 14:50:46

标签: vaadin vaadin7

我试图按下按钮点击文件,按钮出现在网格列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();");
        }
    }
}));

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个。     按钮downloadButton = new Button();     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);