How to download file from server in Vaadin7?

Boris Mitioglov picture Boris Mitioglov · Apr 2, 2013 · Viewed 11.9k times · Source

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

Answer

Boris Mitioglov picture Boris Mitioglov · Apr 3, 2013

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