Downloading Files

Hello!

So, I’ve spent the last while trying to get a JFileChooser to work just right. As things currently stand, the save dialog does open, but it is behind the Vaadin application; I have to alt-tab to access it. I have tried several methods of bringing focus to the chooser, but none have been successful; really makes me wish JFileChoosers had a bringtofront() method like Vaadin windows, but shigata ga nai (sorry, been reading Shogun lately, they say that a LOT)?

To reiterate, I have a Save Dialog window from a JFileChooser element, but the window is not automatically in the front, visible and easily used.

To make matters slightly more confusing to a Vaadin newbie such as myself, if I call the chooser a second time after bringing it into focus and closing the thing, the dialog appears in the front just as I’d like! I have to restart the local Tomcat server to reproduce the issue on my computer.

Any help that could be given would be greatly appreciated.

JFileChooser? That is a swing component, you can not use it with vaadin because your vaadin front-end is a browser and not a desktop.

If you want to choose a file to upload from the client (computer with the browser) to the server, look at the Upload component. Note that it cannot be customized much due in part to browser and security restrictions.

If you want to download a file from the server to the client and decide where to place it in the client file system, search the forum for threads on downloading resources. Again, it is the browser that decides exactly how to ask for the location etc. and the server can only give “hints” such as “try to save this instead of displaying directly in the browser”.

To get around the above mentioned limitations, you would need to use native code on the browser side (use or implement a native browser plug-in).

If you want e.g. to choose a location on the server, you need to implement the location selection yourself, but this can be done entirely using standard Vaadin components. Be careful about introducing security holes in that case - you should not allow the user to e.g. browse the whole file system or replace a system file.

Thank you for your responses!

Yes, I do not believe that what I want to do can be accomplished with the Upload feature. My end goal is to export a file to the user’s choice of location, the information being formatted by JExcelApi. Basically, at this point, all that I need is a user-chosen file path to feed as the file location, such as their Desktop or Documents folder. My understanding is that the Upload class specifically does not make this information available, and I was unsurprisingly unable to work around that fact, so I turned to the old JFileChooser, finding that it worked, but without focus. Technically, I am not even downloading a file as much as I am creating one, as the file does not exist until the button is pressed.

I will try to do a bit more forum-delving on the subject, as you suggested. Thanks again for your reply.

You should consider this a download from point of view of the browser and therefore the application even though the content may be dynamically created and maybe even never exists on the server. There should be examples downloading a file generated on the fly (StreamResource, …). on the forum.

If the APIs you use only generate files on the disk (- not output streams etc.), you could give a temporary location on the server file system and then provide the file to download from there.

Thank you very much for pointing me in the right direction! I got sidetracked by other priorities for a while, but I came back and read your response and in no time at all I had the download working. I hope you don’t mind if I expand upon my solution in the hopes that specifics help some who follow.

	public void clickEvent( ClickEvent event ) {

		//Dynamically retrieve the base path of the program
		String basepath = getApplication().getContext().getBaseDirectory().getAbsolutePath();
		FileResource resource;

		//Run the non-Vaadin code to generate the file
		fileGenerator.generateFile();
		
		//Initiate the file resource to the newly-generated file at its generated location of "src/main/webapp/tmp/generatedfile.filetype"
		resource = new FileResource(new File(basepath + "/tmp/generatedfile.filetype"), getApplication());
		
		//Open the generated file for download
		open(resource);
	}

It is not working in vaadin 7

Maybe
this tutorial
helps with Vaadin 7.

Thank you for your advice. It really helps me.

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