Download zip file

Hi everyone,

I have this code:

        downloadButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        downloadButton.addClickListener(event -> {
            byte[] zipFileData = createZipFile();
            if (zipFileData != null) {
                StreamResource streamResource = new StreamResource("output.zip", new InputStreamFactory() {
                    @Override
                    public InputStream createInputStream() {
                        return new ByteArrayInputStream(zipFileData);
                    }
                });
                String requestUrl = UI.getCurrent().getPage().toString();
                String downloadUrl = requestUrl + "/download/output.zip";
                downloadButton.getElement().setAttribute("href", downloadUrl);
                downloadButton.getElement().setAttribute("download", "output.zip");


                Notification.show("ZIP file downloaded");
            } else {
                Notification.show("Error creating ZIP file");
            }
        });

        add(downloadButton);```

At the end of process I get the notification "ZIP file downloaded", but I do not have no one zip to download. How can I solve? I use Vaadin 14.

I want it to start automatically

You need to have user interaction (like a click) in order to start a download

and I thought that what you were attempting to do with the click listener anyway?

The user interact with the download button, and I don’t want to use an anchor to save the .zip file on the local machine

You can place the button inside the Anchor

Just keep the anchor text empty

Thanks! it works :smile: