Hi Olli & all. I have used this add-on a few times and it is excellent. Until now I have always had the file available before creating the UI, so I could use the approach in the Code Samples.
However, now I have a use case where I have a “download” button in the UI, but I don’t want to generate the actual file until the button is clicked.
I have a work around were I download the file, THEN create a new button on the UI, but that just seems wrong.
Can anyone suggest a code sample / syntax where I don’t have get the file until it is asked for?
I have a similar problem at the moment. I’m using FileDownloadWrapper.setResource to update the StreamResource file name in advance. The InputStreamFactory is in a lambda so I don’t think it’s needed to be replaced but it will be to place a new name in.
Thx Kevin. I appreciate the info. I’ve ended up just using 2 buttons. Clunky, but at least I understand the familiar J5 syntax ;-). Lambda syntax is not particularly hard when you see it, but it is rarely explained well by advanced users.
In the process of clicking on the wrapped component, the wrapped component gets focus befor the click event is processed. You can trigger a call to the server, to set the file name in the download attribute of the anchor that wraps the button.
In code I wrote for a similar purpose, I change the file name to have a current time stamp.
Anchor anchor = new Anchor(href, "");
Button button = new Button(label, new Icon(VaadinIcon.DOWNLOAD_ALT));
anchor.add(button);
button.addFocusListener(e -> {
String dlName = prefix + "_" + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE) + ".xls";
anchor.getElement().setAttribute("download", dlName);
});
If using the wrapper, you can probably call the setFileName method to change the download name at the very last moment – from a quick read of the code, it just sets the value of the download attribute exactly like the snippet above does.