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

Bor*_*lov 7 java download vaadin

我有FileResource

FileResource curResource = new FileResource(new File(basepath +
                                "/WEB-INF/docs/"+path+".pdf"));
Run Code Online (Sandbox Code Playgroud)

我想通过单击按钮从计算机上的浏览器保存此文件.我怎么能在Vaadin 7中这样做?谢谢

我尝试这样的事情:

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

但我有空:空白页面没有任何反应......

Bor*_*lov 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);
Run Code Online (Sandbox Code Playgroud)

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


小智 8

此解决方案的问题在于:在调用fd.extend之前必须知道文件名和文件内容.

如果要按需构建文件名和文件内容,请参阅Vaadin wiki页面中的教程:让用户下载文件