Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Download File
I have form to upload file on win server. So next I want to download that file.
How can I donwload that file?
Use a StreamResource and FileDownloader. Wiki article written for 7.0.0 https://vaadin.com/wiki/-/wiki/Main/Letting+the+user+download+a+file
About resources in book of Vaadin https://vaadin.com/book/-/page/application.resources.html#application.resources.stream
Also I have to mention that the popular Viritin add-on has a DownloadButton, that does some of the work for you.
Johannes Häyry: Use a StreamResource and FileDownloader. Wiki article written for 7.0.0 https://vaadin.com/wiki/-/wiki/Main/Letting+the+user+download+a+file
About resources in book of Vaadin https://vaadin.com/book/-/page/application.resources.html#application.resources.stream
FileDownloader cannot be resolved to a type...
What should I do..?
It's com.vaadin.server.FileDownloader available in vaadin-server jar. There since 7.0.0. Are you using Vaadin 6 or 7?
Johannes Häyry: It's com.vaadin.server.FileDownloader available in vaadin-server jar. There since 7.0.0. Are you using Vaadin 6 or 7?
There is not com.vaadin.server package, and I'm using 6.8.4v
Hi
Works fine for 6.8.16.
Wrapper class:
public class MyStreamResource extends StreamResource {
private static final long serialVersionUID = 1;
private DownloadStream downloadStream;
public MyStreamResource(StreamSource streamSource, String filename, Application application) {
super(streamSource, filename, application);
}
@Override
public DownloadStream getStream() {
final StreamSource streamSource = getStreamSource();
if (streamSource == null) {
return null;
}
if (downloadStream == null) {
downloadStream = new DownloadStream(streamSource.getStream(), getMIMEType(), getFilename());
downloadStream.setBufferSize(getBufferSize());
downloadStream.setCacheTime(getCacheTime());
}
return downloadStream;
}
}
Use:
StreamResource resource = new MyStreamResource(new StreamResource.StreamSource() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getStream() {
InputStream fis = null;
try {
fis = <You input stream>
} catch (FileNotFoundException ex) {
log.error("Error getting file", ex);
}
return fis;
}
}, "myfile", getApplication());
resource.setMIMEType("application/octet-stream");
resource.setCacheTime(0);
resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + "myfile" + "\"");
resource.getStream().setParameter("Content-Transfer-Encoding", "binary");
getMainWindow().open(resource, "_self");
Andris Lapinsh: Hi
Works fine for 6.8.16.Wrapper class:
public class MyStreamResource extends StreamResource { private static final long serialVersionUID = 1; private DownloadStream downloadStream; public MyStreamResource(StreamSource streamSource, String filename, Application application) { super(streamSource, filename, application); } @Override public DownloadStream getStream() { final StreamSource streamSource = getStreamSource(); if (streamSource == null) { return null; } if (downloadStream == null) { downloadStream = new DownloadStream(streamSource.getStream(), getMIMEType(), getFilename()); downloadStream.setBufferSize(getBufferSize()); downloadStream.setCacheTime(getCacheTime()); } return downloadStream; } }
Use:
StreamResource resource = new MyStreamResource(new StreamResource.StreamSource() { private static final long serialVersionUID = 1L; @Override public InputStream getStream() { InputStream fis = null; try { fis = <You input stream> } catch (FileNotFoundException ex) { log.error("Error getting file", ex); } return fis; } }, "myfile", getApplication()); resource.setMIMEType("application/octet-stream"); resource.setCacheTime(0); resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"" + "myfile" + "\""); resource.getStream().setParameter("Content-Transfer-Encoding", "binary"); getMainWindow().open(resource, "_self");
Thanks it's working :D
Why are you using vaadin 6? It's only a matter of curiosity :p
I suspect that it is necessary to maintain the old project or move to the latest version is too expensive in time and money :)
Jon Inazio Sánchez Martínez: Why are you using vaadin 6? It's only a matter of curiosity :p
It was started already, I'm just fixing some sh*t overhere, who left my cowerker, now ex-coworker :P
Btw I don't know how to upgrade to lates version, and I'm not sure if it will works again if I upgrade it.
I'm scared of getting lot of errors :D