DownloadComponent (Flow)

Hi everyone,

I have a filename, mimeType and data in byte and want a Button where the user can download this dynamically created file.
Until Vaadin 8 there has been e.g. FileDownloader [1]
.
In Vaadin 10 (Flow/Java) I can create a StreamResource [2]
but I don’t find the way how to trigger the download.
Any help or example is welcome.

Thanks and greetings, Björn

[1]
com.vaadin.server.FileDownloader

[2]
com.vaadin.flow.server.StreamResource

Hi,

To link your stream resource to UI, you can add an Anchor and setHref(yourStreamResource).

@Id("youranchor")
private Anchor youranchor;

private void init(){
	youranchor.setHref(resource);
}



(and my html file has: <a id="youranchor" class="button"><iron-icon icon="vaadin:calc-book"></iron-icon>Export Excel</a>

Does it help ?

PS: here an old post https://vaadin.com/forum/thread/17061112/download-link-or-button

Hi Jean-Christophe,

thanks for the hint to ‘Anchor’, example and link to the old post which I really didn’t find.

But … what I want to achieve is that I have a Button and only on click several things shall be done which result in the bytes which should be offered as download.

So using anchor would allow me to have the Button-click and than open a new dialog where the Anchor to the download could be offered.

Is there any solution where a Button clickListener can trigger first internal calculation and than offer a resulting download without an additional Dialog? That would be great.

Thanks, Björn

Hi Bjoern,

I’m not sure if I understand your need correctly.
You want a button and when you click on it you can generate your file (dynamically) and download it ?

In that case you can:

  • Use this anchor and add class=“button” (with some CSS :p) and the anchor looks like a button.
  • When click on my anchor, file is generated (the streamresource is built when you click on the anchor) and downloaded.

I don’t know if you can use directly a button (I did that on vaadin 10 beta so I didn’t look for other solution).

Hi Jean-Christophe,

thank you! Yes, that is what I was looking for. I didn’t know that the StreamResource is build after the button was clicked. Than that is my solution. :slight_smile:

Thanks, Björn

For easy of use you can try this add-on from a Vaadin employee:

https://vaadin.com/directory/component/file-download-wrapper