I have a dialog in which the user chooses the format of the file to be downloaded (CSV, PDF) from a combo box and then hits the OK button to begin the download. I currently have a method that gets called from the combo box’s ValueChangeListener that looks something like this:
ExportedReportDataSource dataSource = new ExportedReportDataSource(response);
StreamResource resource = new StreamResource(dataSource, filename);
resource.setMIMEType("application/" + exportFormat.getValue());
resource.getStream().setParameter("Content-Disposition", "attachment; filename="+filename);
FileDownloader downloader = new FileDownloader(resource);
downloader.extend(okButton);
The problem is that as I select different formats, more and more downloaders get called whenever the OK button is selected! I need a way to “unextend” the previous downloader’s association with the OK button.
I hope my problem statement is clear enough. Any info is much appreciated.
Why do you create new and new FileDownloader every time, the combo value changed? Use just com.vaadin.server.FileDownloader.setFileDownloadResource(Resource) method in your listener.
…or you do something like this:
new FileStream(getStream()).extend(button);
...
public StreamResource getStream(){
StreamResource sr;
//Create your StremResource dynamically.
return sr;
}
IMPORTANT: One thing that is mandatory for this to work is to use different filenames for every downloaded file, so that we don’t use the browser cache.