Download file problem

Hello,
I am trying to download a file (say c:/test) from the server to client.
Now it shows the content of the file inside the panel.
But I would like it to show the browser “save/open” window so that the user can choose to save it.
I saw a few posts about it, but did not really find the solution that works.
By the way, it works fine on firefox, but not on explorer.
What am I missing here?
Any help would be appreciated.


			     Panel myPanel = new Panel("my panel");
			     Embedded embedded = new Embedded();
			     FileResource fResource = new FileResource(new File("C:/test"), getApplication());
			     embedded.setSource(fResource); 
			     fResource.getStream().setParameter("Content-Disposition", "attachment; filename=" + "test");
			     //embedded.setMimeType("application/octet-stream");
			     embedded.setType(Embedded.TYPE_BROWSER);
			     myPanel.addComponent(embedded);

Regards,
Momena

I actually found the solution right after I posted the question.
I got it from an earlier post, for some reason it did nto work before.
I had to extend the FileResourse as below:



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.vaadin.Application;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.FileResource;
	
	public class FileDownloadResource  extends FileResource{

	public FileDownloadResource(File sourceFile, Application application) {
		super(sourceFile, application);
	}

	public DownloadStream getStream() {
		try {
			final DownloadStream ds = new DownloadStream(new FileInputStream(
					getSourceFile()), getMIMEType(), getFilename());
			ds.setParameter("Content-Disposition", "attachment; filename="
					+ getFilename());
			ds.setCacheTime(getCacheTime());
			return ds;
		} catch (final FileNotFoundException e) {
			//TODO:do something
			return null;
		}
	}
}

My main code looks like this:


FileDownloadResource f = new FileDownloadResource(new File("C:/test"), getApplication());
getWindow().open(f);

Hope this helps someone.

Regards,
Momena

Momena,

thanks for providing your solution - it has really helped!

S.

Why don’t you use Link instead of Embedded?

I have noticed that Google Chrome aggressively caches older copies of files when I use the above FileDownloadResource sample, and then open the file from the main window as follows:

				FileDownloadResource downloadResource = new FileDownloadResource(
						new File(filePathName), getApplication());
				event.getButton().getWindow().open(downloadResource, "_blank");

Is there a way to force it to refresh its cached copy every time? Firefox isn’t as bad about this. I want to make sure that the user does not get the old version of the file whenever I send it to the client this way.

Thanks in advance,
David

Does anyone know of a way to force Chrome to update its cache with the new copy of the file each time open() is called? If not, is there another way to accomplish this? Thanks!

You can try to set the cacheTime to zero on your FileResource.
Or you could alter the filename, e.g. by adding a counter or date+time.