Vaadin 14 文件下载按钮单击不起作用
Vaadin 14 File downloading on Button Click not works
我正在使用 https://vaadin.com/directory/component/file-download-wrapper
使用以下代码
exportButton = new Button("export"));
FileDownloadWrapper buttonWrapper = new FileDownloadWrapper(
new StreamResource(genericGridView.getExportFileCaption(), () -> getExportStream()));
buttonWrapper.wrapComponent(exportButton);
buttonLayout.add(exportButton); //Button Layout is an Horizontal Layout which is added finally in the
main Layout.
并且这部分代码是在我的视图加载时编写的。但是方法 getExportStream 方法没有被执行(试图在 eclipse 中调试但它从未执行过)既没有在视图加载中也没有在按钮单击事件中。
请你帮忙实现这个,我这里是不是做错了什么?
对我来说,使用 FileDownloadWrapper
的代码看起来是正确的。所以我倾向于认为问题更多地与你如何生成数据有关。我从我的测试应用程序中复制了一个有效的代码片段。此处的 CSV 导出使用来自 com.opencsv.opencsv
库的 StatefulBeanToCsv
和 StatefulBeanToCsvBuilder
。
private void export(Grid<Person> grid, TextArea result) {
Set<Person> selection = grid.asMultiSelect().getValue();
Stream<Person> persons = persons = selection.stream();
StringWriter output = new StringWriter();
StatefulBeanToCsv<Person> writer = new StatefulBeanToCsvBuilder<Person>(
output).build();
try {
writer.write(persons);
} catch (CsvDataTypeMismatchException
| CsvRequiredFieldEmptyException e) {
output.write("An error occured during writing: " + e.getMessage());
}
result.setValue(output.toString());
StreamResource resource = new StreamResource("export.csv",
() -> new ByteArrayInputStream(output.toString().getBytes()));
FileDownloadWrapper download = new FileDownloadWrapper(resource);
Button button = new Button("Click to download");
download.wrapComponent(button);
add(download);
}
您需要将 FileDownloadWrapper 添加到布局而不是 Button。否则,FileDownloadWrapper 什么都不做。
我正在使用 https://vaadin.com/directory/component/file-download-wrapper 使用以下代码
exportButton = new Button("export"));
FileDownloadWrapper buttonWrapper = new FileDownloadWrapper(
new StreamResource(genericGridView.getExportFileCaption(), () -> getExportStream()));
buttonWrapper.wrapComponent(exportButton);
buttonLayout.add(exportButton); //Button Layout is an Horizontal Layout which is added finally in the
main Layout.
并且这部分代码是在我的视图加载时编写的。但是方法 getExportStream 方法没有被执行(试图在 eclipse 中调试但它从未执行过)既没有在视图加载中也没有在按钮单击事件中。
请你帮忙实现这个,我这里是不是做错了什么?
对我来说,使用 FileDownloadWrapper
的代码看起来是正确的。所以我倾向于认为问题更多地与你如何生成数据有关。我从我的测试应用程序中复制了一个有效的代码片段。此处的 CSV 导出使用来自 com.opencsv.opencsv
库的 StatefulBeanToCsv
和 StatefulBeanToCsvBuilder
。
private void export(Grid<Person> grid, TextArea result) {
Set<Person> selection = grid.asMultiSelect().getValue();
Stream<Person> persons = persons = selection.stream();
StringWriter output = new StringWriter();
StatefulBeanToCsv<Person> writer = new StatefulBeanToCsvBuilder<Person>(
output).build();
try {
writer.write(persons);
} catch (CsvDataTypeMismatchException
| CsvRequiredFieldEmptyException e) {
output.write("An error occured during writing: " + e.getMessage());
}
result.setValue(output.toString());
StreamResource resource = new StreamResource("export.csv",
() -> new ByteArrayInputStream(output.toString().getBytes()));
FileDownloadWrapper download = new FileDownloadWrapper(resource);
Button button = new Button("Click to download");
download.wrapComponent(button);
add(download);
}
您需要将 FileDownloadWrapper 添加到布局而不是 Button。否则,FileDownloadWrapper 什么都不做。