Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file from server in Vaadin7?

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

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

like image 462
Boris Mitioglov Avatar asked Apr 02 '13 11:04

Boris Mitioglov


2 Answers

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

like image 166
Boris Mitioglov Avatar answered Nov 08 '22 08:11

Boris Mitioglov


The problem of this solution is that: The file name and file content must be known before you call the 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

like image 29
Nhat Nam NGUYEN Avatar answered Nov 08 '22 09:11

Nhat Nam NGUYEN