如何在Vaadin7中从服务器下载文件?

7
我有一个FileResource。
FileResource curResource = new FileResource(new File(basepath +
                                "/WEB-INF/docs/"+path+".pdf"));

我希望通过点击按钮在浏览器上将此文件保存到计算机中。如何在Vaadin 7中实现?谢谢。

我尝试了类似以下的方法:

ExternalResource resource = new ExternalResource(basepath +
                                "/WEB-INF/icons/"+"block_16.png");
Page.getCurrent().open(resource.getURL(),"Download",true);

但是我只看到一个空的about:blank页面,什么也没有发生...

之前的帖子可能会有所帮助 http://stackoverflow.com/questions/8169284/vaadin-downloaded-file-has-whole-path-as-file-name - AurA
@AurA,我已经看过这篇文章了,但它并没有帮助我,因为它是针对 Vaadin 的旧版本。在 Vaadin7 中,open 方法只接收 URL 作为第一个参数,而不是 StreamResource =( - Boris Mitioglov
2个回答

10

我解决了我的问题!

private String basepath = VaadinService.getCurrent()
            .getBaseDirectory().getAbsolutePath();
private Button saveExcel = new Button();
Resource res = new FileResource(new File(basepath +
                "/WEB-INF/docs/settings.xlsx"));
FileDownloader fd = new FileDownloader(res);
fd.extend(saveExcel);

Vaadin中从服务器下载非常容易


8
这个解决方案的问题在于:在调用fd.extend之前必须知道文件名和文件内容。
如果您想根据需要构建文件名和文件内容,请参见Vaadin维基页面中的教程:让用户下载文件

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接