Download file on button click in Vaadin 12

Hello, I have a Dialog with Button called “Download”, and I want on user click on this button start a file downloading.

so…

		// the button
        Button downloadButton = new Button("Download");
        // file data
		byte[] data = getContent();
		// stream resource
        StreamResource streamResource = new StreamResource("fileName.extName", () -> new ByteArrayInputStream(data));

        downloadButton.addClickListener(event -> {
			// ????
        });

All examples I found in Google related to Vaadin 7 or Vaadin 8 with classes like FileDownloader or something like that: Page.getCurrent().open(…);
This classes missing in Vaadin 12.

Maybe I need wrap my button with anchor or vice-versa, I don’t know, help me please. I need a button, on click browser must start file download and do not break the current page.

Thank you in advance, %username%!

Hi Андрей Крюков

There is an add-on that does exactly what you need: [FileDownloadWrapper]
(https://vaadin.com/directory/component/file-download-wrapper). You can find a code sample using a button [here]
(https://vaadin.com/directory/component/file-download-wrapper/samples)

Cheers

Wow, thank you a lot!