简体   繁体   English

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

[英]How to download file from server in Vaadin7?

I have FileResource 我有FileResource

FileResource curResource = new FileResource(new File(basepath +
                                "/WEB-INF/docs/"+path+".pdf"));

and i want save this file from browser on the computer by click the button. 我想通过单击按钮从计算机上的浏览器保存此文件。 How can i do this in Vaadin 7? 我怎么能在Vaadin 7中这样做? Thanks 谢谢

I try something like this: 我尝试这样的事情:

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

but i have empty about:blank page and nothing happens... 但我有空:空白页面没有任何反应......

I resolve my problem! 我解决了我的问题!

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);

It's so easy to download from server in Vaadin 从Vaadin的服务器下载非常容易

The problem of this solution is that: The file name and file content must be known before you call the fd.extend. 此解决方案的问题在于:在调用fd.extend之前必须知道文件名和文件内容。

If you want to build the file name and file content on demand, see the tutorial in Vaadin wiki page: Letting The User Download A File 如果要按需构建文件名和文件内容,请参阅Vaadin wiki页面中的教程: 让用户下载文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM