I’m using a DownloadHander.fromInputStream from Vaadin 24.9.4 with java 24 and springboot 3.5.7.
When the handler is called and the file doesn’t exists I’m returning a 404:
return DownloadResponse.error(404, "The selected file is not available: %s".formatted(tarBallFile.getPath()));
The browser (chrome) then displays a failed message in the download list:
I’m expecting the browser to display a 404 error.
It would also be nice to be able to control the filename when creating the error so the browser doesn’t show ‘download.txt’.
Here is the complete method (slightly edited)
return new ComponentRenderer<>(version ->
{
// Define the download handler
DownloadHandler downloadHandler = DownloadHandler.fromInputStream(_ ->
{
try
{
var fileStore = FileStoreFactory.getFileStore(version);
File tarBallFile = fileStore.fetchTarBall(version);
if (!tarBallFile.exists())
{
return DownloadResponse.error(404, "The selected file is not available: %s"
.formatted(tarBallFile.getPath()));
}
// Get file name and guess content type
String fileName = this.file.getName() + "_" + version.getVersion() + ".tar.gz";
String contentType = "application/gzip";
// Return a DownloadResponse with the InputStream
InputStream inputStream = new FileInputStream(tarBallFile);
return new DownloadResponse(inputStream, fileName, contentType, tarBallFile.length());
}
catch (Exception e)
{
// Return an error response
return DownloadResponse.error(500);
}
});
