Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileDownloader: Dynamically generated files not generated in Firefox due to caching #10909

Closed
mirkoliver opened this issue May 11, 2018 · 7 comments

Comments

@mirkoliver
Copy link

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?

@mirkoliver
Copy link
Author

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?

@mobileWMS
Copy link

I have the same problem ( Vaadin 7 ).

@mcollovati
Copy link
Contributor

Have you tried with StreamResource.setCacheTime(0)?

@mobileWMS
Copy link

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

@mirkoliver
Copy link
Author

Great! Thanks for the hint. This works!

@ideat
Copy link

ideat commented May 28, 2018

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);

@mobileWMS
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants