You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ismael Hasan <kr...@gmail.com> on 2011/11/14 12:39:01 UTC

How to create a download link that is not cached by the browsers?

Hello,

I want to make a DownloadLink for a dynamic resource, a file generated on
the fly; to do this, I overwrite the "onClick" method of DownloadLink so
the file is generated whenever the link is clicked. In the context of my
problem, it should happen that 2 clicks on the link should return different
files without reloading the page.

The problem I am facing is that the browser caches the link by its "src"
attribute, so the "onClick" method is only called the first time: with the
next clicks the browser directly returns the file generated in the first
"onClick" (and previously downloaded).

Is there a clean solution to this problem? The solutions I thought so far
are pretty dirty:

1.- Instead of having a link, have a list of links, generate a new link
whenever the link is clicked, and refresh the list via Ajax, so instead of
a "dynamic" link there will be a new link each time.
2.- Add to the link a "foo" parameter each time it is clicked, so the
browser sees it as a different link. I don't know how to do this, any
insight will be appreciated.


Thank you for your attention,

Ismael.

Re: How to create a download link that is not cached by the browsers?

Posted by Chantal Ackermann <ch...@btelligent.de>.
Hi Ismael,

here is the code for a non-cached download link. (I don't know whether
this really solves your problem but just in case.)


final NamesCsvStreamWriter stream = new NamesCsvStreamWriter();
ResourceStreamResource rsr = new ResourceStreamResource(stream) {
	@Override
	protected ResourceResponse newResourceResponse(Attributes attributes) {
		stream.initContent(provider.getFilteredList());
		return super.newResourceResponse(attributes);
	}

	@Override
	protected void configureCache(ResourceResponse data,
			Attributes attributes) {
		Response response = attributes.getResponse();
		((WebResponse)response).disableCaching();
	}
};
rsr.setContentDisposition(ContentDisposition.ATTACHMENT);
rsr.setTextEncoding(UTF_8);
rsr.setFileName("export.csv");
form.add(new ResourceLink<ResourceStreamResource>("export", rsr));


Cheers,
Chantal


On Mon, 2011-11-14 at 12:39 +0100, Ismael Hasan wrote:
> Hello,
> 
> I want to make a DownloadLink for a dynamic resource, a file generated on
> the fly; to do this, I overwrite the "onClick" method of DownloadLink so
> the file is generated whenever the link is clicked. In the context of my
> problem, it should happen that 2 clicks on the link should return different
> files without reloading the page.
> 
> The problem I am facing is that the browser caches the link by its "src"
> attribute, so the "onClick" method is only called the first time: with the
> next clicks the browser directly returns the file generated in the first
> "onClick" (and previously downloaded).
> 
> Is there a clean solution to this problem? The solutions I thought so far
> are pretty dirty:
> 
> 1.- Instead of having a link, have a list of links, generate a new link
> whenever the link is clicked, and refresh the list via Ajax, so instead of
> a "dynamic" link there will be a new link each time.
> 2.- Add to the link a "foo" parameter each time it is clicked, so the
> browser sees it as a different link. I don't know how to do this, any
> insight will be appreciated.
> 
> 
> Thank you for your attention,
> 
> Ismael.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to create a download link that is not cached by the browsers?

Posted by Martin Grigorov <mg...@apache.org>.
https://issues.apache.org/jira/browse/WICKET-4225

On Mon, Nov 14, 2011 at 2:10 PM, Martin Grigorov <mg...@apache.org> wrote:
> Hi,
>
> Today another user complained to me with the same problem.
> I'm working on an improvement.
>
> On Mon, Nov 14, 2011 at 1:39 PM, Ismael Hasan <kr...@gmail.com> wrote:
>> Hello,
>>
>> I want to make a DownloadLink for a dynamic resource, a file generated on
>> the fly; to do this, I overwrite the "onClick" method of DownloadLink so
>> the file is generated whenever the link is clicked. In the context of my
>> problem, it should happen that 2 clicks on the link should return different
>> files without reloading the page.
>>
>> The problem I am facing is that the browser caches the link by its "src"
>> attribute, so the "onClick" method is only called the first time: with the
>> next clicks the browser directly returns the file generated in the first
>> "onClick" (and previously downloaded).
>>
>> Is there a clean solution to this problem? The solutions I thought so far
>> are pretty dirty:
>>
>> 1.- Instead of having a link, have a list of links, generate a new link
>> whenever the link is clicked, and refresh the list via Ajax, so instead of
>> a "dynamic" link there will be a new link each time.
>> 2.- Add to the link a "foo" parameter each time it is clicked, so the
>> browser sees it as a different link. I don't know how to do this, any
>> insight will be appreciated.
>>
>>
>> Thank you for your attention,
>>
>> Ismael.
>>
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to create a download link that is not cached by the browsers?

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Today another user complained to me with the same problem.
I'm working on an improvement.

On Mon, Nov 14, 2011 at 1:39 PM, Ismael Hasan <kr...@gmail.com> wrote:
> Hello,
>
> I want to make a DownloadLink for a dynamic resource, a file generated on
> the fly; to do this, I overwrite the "onClick" method of DownloadLink so
> the file is generated whenever the link is clicked. In the context of my
> problem, it should happen that 2 clicks on the link should return different
> files without reloading the page.
>
> The problem I am facing is that the browser caches the link by its "src"
> attribute, so the "onClick" method is only called the first time: with the
> next clicks the browser directly returns the file generated in the first
> "onClick" (and previously downloaded).
>
> Is there a clean solution to this problem? The solutions I thought so far
> are pretty dirty:
>
> 1.- Instead of having a link, have a list of links, generate a new link
> whenever the link is clicked, and refresh the list via Ajax, so instead of
> a "dynamic" link there will be a new link each time.
> 2.- Add to the link a "foo" parameter each time it is clicked, so the
> browser sees it as a different link. I don't know how to do this, any
> insight will be appreciated.
>
>
> Thank you for your attention,
>
> Ismael.
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org