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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15762991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:47:04  来源:igfitidea点击:

How to download file from server in Vaadin7?

javadownloadvaadin

提问by Boris Mitioglov

I have 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? Thanks

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

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...

但我有空的 about:blank page 并且没有任何反应...

回答by Boris Mitioglov

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 从服务器下载是如此简单

回答by Nhat Nam NGUYEN

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 页面中的教程:让用户下载文件