Daniel833
(Daniel Becker)
April 30, 2019, 1:37pm
1
Greetings,
I have a Vaadin application where the user is able to show a searchresult in a grid with details.
The customer wants a button to get this result as a csv oder excel but it shouldn’t look like the grid exactly.
I’ve build a function to build an textbased output for this purpose.
Now I’m looking for a function to get this file as a download.
In Vaadin Framework there was the FileDownloader but it is missing in flow.
How can I provide an download, or a possibility for the user to get the grid?
Regards Daniel
Kaspar4
(Kaspar Scherrer)
April 30, 2019, 1:46pm
2
Hi Daniel
I like to use Olli’s add-on for this: [File Download Wrapper]
(https://vaadin.com/directory/component/file-download-wrapper/discussions ). It’s easy to use, and you can find code samples in the link under the tab ‘code samples’.
Eugene60
(Eugene Ramirez)
April 30, 2019, 3:02pm
3
Below is what I use. I got the code from somewhere and modified it a bit as I download files from Amazon S3 as well. I did look at the File Download Wrapper as well but opted for the solution below.
public class DownloadLink extends Anchor {
public DownloadLink(File file) {
Anchor anchor = new Anchor(getStreamResource(file.getName(), file), file.getName());
anchor.getElement().setAttribute("download", true);
anchor.setHref(getStreamResource(file.getName(), file));
add(anchor);
}
public DownloadLink(LpAttachment lpAttachment) {
StreamResource streamResource=getStreamResource(lpAttachment);
Anchor anchor = new Anchor(streamResource, "");
anchor.getElement().setAttribute("download", true);
anchor.setHref(streamResource);
anchor.add(new Button(new Icon(VaadinIcon.DOWNLOAD_ALT)));
add(anchor);
}
public StreamResource getStreamResource(LpAttachment lpAttachment) {
return new StreamResource(lpAttachment.getVirtualFilename(), () -> {
try {
SystemProperty s3BucketName = "bucket name";
String key= "key";
InputStream is= VLpUtil.getS3Stream(s3BucketName.getValue(), key);
BufferedInputStream bis=new BufferedInputStream(is);
return bis;
} catch (LPApplicationException e) {
MessageDialog.show(e);
}
return null;
});
}
public StreamResource getStreamResource(String filename, File content) {
return new StreamResource(filename, () -> {
try {
return new BufferedInputStream(new FileInputStream(content));
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
}
}
Use a component renderer to add it to the grid. Below is what I do as I add two of them:
grid.addColumn(new ComponentRenderer<>(lpa -> {
HorizontalLayout hl=new HorizontalLayout();
DownloadLink downloadLink=new DownloadLink(lpa);
if (isOwner) {
Button buttonRemove=new Button();
buttonRemove.setIcon(VaadinIcon.FILE_REMOVE.create());
buttonRemove.addClickListener(event->processRemove(lpa));
hl.add(downloadLink,buttonRemove);
}else {
hl.add(downloadLink);
}
return hl;
})).setWidth("100px").setFlexGrow(0).setHeader("Actions");
Daniel833
(Daniel Becker)
May 3, 2019, 1:25pm
4
Hi,
the last one was perfect. Also for learning how the Anchor works.
Thanks