Framework: FileDownloader: Dynamically generated files not generated in Firefox due to caching

Created on 11 May 2018  路  7Comments  路  Source: vaadin/framework

I'm using the Vaadin FileDownloader.

When clicking on the corresponding button for the first time, Firefox caches the downloaded file. If I'm not refreshing the screen, the second time the browser doesn't contact FileDownloader anymore but immediately offers the cached file.

The files I'm downloading are dynamically generated before download - so I need an option to switch off this browser behaviour.

I've found this link: https://stackoverflow.com/questions/12331993/how-to-disable-browser-caching-in-vaadin?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa ...

... but how can I do this with FileDownloader?

Most helpful comment

Have you tried with StreamResource.setCacheTime(0)?

All 7 comments

I do it exactly like in the example under "LAZILY DETERMINE THE CONTENT AND THE NAME OF THE FILE BEING SERVER" on https://vaadin.com/docs/v8/framework/articles/LettingTheUserDownloadAFile.html. But when I'm in my Firefox and click the second time (see description above), the overriden method handleConnectorRequest isn't any more called and therefore there is no chance to change the filename. Therefore I assume, that it has to do something with caching. But how to force the browser not to cache in this case?

I have the same problem ( Vaadin 7 ).

Have you tried with StreamResource.setCacheTime(0)?

yes, in my case FileResource.setCacheTime(0); fixed my Firefox issue.

Great! Thanks for the hint. This works!

Hi, thanks for you answer, please you can can share part of your code, still have the problem

I, do this, it's correct?
final Resource res = new FileResource(new File(this.getClass().getClassLoader().getResource("/template/").getPath() + "extract_"+latePayment.getStudentId().toString()+".xlsx"));
((FileResource) res).setCacheTime(0);

I literally just called the setCacheTime(0) method, nothing much else. Here is some code:

FileResource csvFileResource = generateCsvDataFile();
csvFileResource.setCacheTime(0);

And generateCsvDataFile() talks to backend process to construct a CSV:

    private FileResource generateCsvDataFile() throws WMSException {
        String fileName = getCsvDataFileName();

        if( fileName == null || fileName.trim().isEmpty() )
            throw new WMSException("No data file to download");

        File csvFile = new File(fileName);
        if( !csvFile.exists())
            throw new WMSException("Data file " + fileName + " does not exist");

        LOGGER.info(
                "File " + csvFile.getName() +
                " at " + csvFile.getParent() +
                ", size " + csvFile.length() );
        FileResource csvFileResource = new FileResource(csvFile);

        return csvFileResource;
    }

Maybe you are having another issue. This issue seems to only happen with Firefox, and is easily repeatable - every single time in a single session it will get the same file you downloaded the first time. It was rather consistent. The above code so far has NOT broken the other browsers, but in my case it actually makes sense to turn off caching anyway. Hope that helps.

Was this page helpful?
0 / 5 - 0 ratings