How to download FileResource from server?

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? 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…

private String basepath = VaadinService.getCurrent()
			.getBaseDirectory().getAbsolutePath();
private Button saveExcel = new Button();
Resource res = new FileResource(new File(basepath +
                "/WEB-INF/icons/settings1.png"));
FileDownloader fd = new FileDownloader(res);
fd.extend(saveExcel);

It’s so easy to download from server in Vaadin :bashful:

Thank you very much! Made my day.

If the resource is dynamic and only known when button is clicked, does any body handle the case? In vaddin 6.x, download can be implemented freely with Window.open(Resource), but Window.open() is removed in version 7.x.

you have to do something like this: new FileDownloader(getStream()).extend(button); ... public StreamResource getStream(){ //create your Streamresource dynamically here return streamresource; } getStream() will only be called when you click the button.

Please see my test code as the following:

		Resource res = new FileResource(new File("c:/report.xls"));
		final FileDownloader fd = new FileDownloader(res);
		
		Button saveExcel = new Button("Download");	
		saveExcel.addClickListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// download resource is generated here	
				...
				fd.setFileDownloadResource(resource);
			}
		});		

		fd.extend(saveExcel);
  1. Download-able resource (like file report.xls) is generated in buttonClick(), so there is no report.xls at the initial time in main flow.
  2. The action of download is triggered earlier than buttonClick() when the button is clicked. In this case, an old version of report (if existed) is downloaded at the first click, then generate a resource, but the new resource can only be downloaded at the next click.

Anything wrong with the code?

  1. Further, if FileDownloader fd is extended to a MenuBar, clicking any menu item would trigger the action of download.

The feature in version 6.x can be implemented as the following:

		Button saveExcel = new Button("Download");	
		saveExcel.addClickListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Window win = getWindow();
				// download resource is generated here	
				...
				win.open(resource);
			}
		});

I would implement the same feature with vaddin7 - click the button or menu item, create a resource, then download. Is there any easy implementation with v7?

A similar way then in Vaadin 6 is unfortunately not available (although the old way should still be there but deprecated).
More Information

The best way to it in your case would be to put the //download resource is generated here in a seperate method (like the getStream one i described) and add it to the FileDownloader like i described it. That should work better then your way. (
Here
is another thread where someone talks about the way you wanted to do it).
For something like menubars there currently is no non-deprecated way of doing it as far as i know.

Why is getStream() only called when you click the button?

The content can be generated dynamically based on data that is only available when the user actually wants to download the file. Also, very often, the user does not actually open/download the generated content so opening a large number of streams on the off chance that one of those might eventually be read is not a good idea, especially if some of the streams are expensive to open.

Yes, I understand that of course, I actually need something like this in my code. What I don’t understand is why getStream doesn’t get called exactly when the button is extended in Marius example. Where is that code placed exactly?