Download f.txt instead of the expected file

Some customers informed us that my Download-Function in a Vaadin-Dialog downloads a file named “f.txt” instead of the expected Excel. It seems it has something to do with closing the Dialoge before the download is finished

See last comment in Downloading a file randomly results in a f.txt file instead of the real file

Or comment in java - How to mimic click event of anchor tag in Vaadin flow? - Stack Overflow

Currently I have the following function. Closing is called before the InputStream is returned - not nice, but I found no other way to handle it.

    Anchor downloadButton = new Anchor(new StreamResource("Excel.xlsx", () -> {
    	close();
    	return getExcelInputStream();
    }), "");
    downloadButton.getElement().setAttribute("download", true);
    downloadButton.add(new Button("Download Excel"));

Does anyone have an Idea whether this caused the issue and if yes, how to fix it?

Closing the dialog - which removes the element, and thus causes the deallocation of the StreamResource - is most likely the cause here, yes. If your workaround works for you, then I’d recommend using that. Another option could be using the DynamicFileDownloader helper from the Viritin add-on: flow-viritin/src/main/java/org/vaadin/firitin/components/DynamicFileDownloader.java at master · viritin/flow-viritin · GitHub

Perfect, thanks for your help :)