Hello everyone,
I am using the FileDownloadWrapper
add-on from the Vaadin directory to programmatically trigger a file download. My implementation works perfectly in Chrome and other browsers, but I’m facing an intermittent “404 Not Found” error specifically in Firefox.
The goal is to have a method that, when called, creates a temporary download button, wraps it with FileDownloadWrapper
, programmatically clicks it to start the download, and then removes it from the UI.
The Issue and Error Message
The process works inconsistently in Firefox. Sometimes the download starts correctly, but other times Firefox displays a “404 Not Found” error page.
Interestingly, when my Firefox browser language was set to Italian, the download failed consistently. After switching the browser language to English, it started working but still fails intermittently.
Here is the error page shown by Firefox:
Looks like there’s a problem with this site
http://localhost:8080/VAADIN/dynamic/resource/1/d4433986-9d56-4df1-8b22-7ee3603d7877/Pdf_scenario_Trauma_Cranico_in_Vigile_Urban.zip might have a temporary problem or it could have moved.
Error code: 404 Not Found
The site could be temporarily unavailable or too busy. Try again in a few moments.
My Implementation
Here is the Java method I am using to trigger the download. It’s designed to add the download component, simulate a click, and then clean it up.
private void triggerDownload(StreamResource resource) {
UI ui = UI.getCurrent();
if (ui == null || detached.get() || ui.isClosing()) {
return;
}
Button downloadButton = new Button();
downloadButton.getStyle().set("display", "none"); // Nasconde il pulsante
FileDownloadWrapper downloadWrapper = new FileDownloadWrapper(resource);
downloadWrapper.wrapComponent(downloadButton);
ui.access(() -> {
this.getContent().add(downloadWrapper);
// Clicca programmaticamente il pulsante per avviare il download
downloadButton.getElement().executeJs("this.click()")
.then(result -> ui.access(() -> {
// Rimuove il wrapper dopo l'avvio del download
if (!detached.get()) {
this.getContent().remove(downloadWrapper);
}
}));
});
}
What I’ve Considered
- Race Condition: My primary suspicion is a race condition. It seems the FileDownloadWrapper is removed from the UI before Firefox has fully initiated the download request for the StreamResource. This could explain why the dynamic resource URL is no longer valid, leading to the 404.
- Browser Behavior: The fact that it’s worse with a non-English browser language and specific to Firefox suggests it might be related to how Firefox handles programmatic clicks and navigation compared to other browsers.
- Component Lifecycle: I’m trying to manage the component lifecycle correctly by adding the wrapper before the click and removing it after, but maybe there’s a better way to ensure the resource is available until the download has started.
Has anyone encountered a similar issue with FileDownloadWrapper and Firefox? Is there a more robust way to programmatically trigger a download that avoids this potential race condition?
Any advice would be greatly appreciated. Thank you!