I have a panel with a submit button. onsubmit, i want to write some text to a file and provide the file for download to the user. Not sure how to go about doing it. If someone can point to a link or code that will really help
-
3Take a look at this post [How to use Wicket's DownloadLink with a file generated on the fly](http://stackoverflow.com/questions/7646270/how-to-use-wickets-downloadlink-with-a-file-generated-on-the-fly) – mvlupan Mar 29 '13 at 18:59
-
[DownloadLink](http://wicket.apache.org/apidocs/1.5/org/apache/wicket/markup/html/link/DownloadLink.html) – TOUDIdel Mar 30 '13 at 13:49
2 Answers
This did not end up being easy. I used the download link, but none of the form variables were getting used by download link as clicking on the download link does not submit the form. it just uses initial values. I had to use ajax onchange event to update the instance variables representing the form components and then use the download link to create a file based on user inputs.

- 10,945
- 34
- 112
- 168
As detailed in an entry on the Wicket wiki you need to create a behaviour that constructs your file which is then triggered from a submit link. This will ensure that the model update is complete before you use it in creation of the file.
A Behavior implementation that creates a request from a resource stream:
public abstract class AJAXDownload extends AbstractAjaxBehavior
{
private boolean addAntiCache;
public AJAXDownload() {
this(true);
}
public AJAXDownload(boolean addAntiCache) {
super();
this.addAntiCache = addAntiCache;
}
/**
* Call this method to initiate the download.
*/
public void initiate(AjaxRequestTarget target)
{
String url = getCallbackUrl().toString();
if (addAntiCache) {
url = url + (url.contains("?") ? "&" : "?");
url = url + "antiCache=" + System.currentTimeMillis();
}
// the timeout is needed to let Wicket release the channel
target.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 100);");
}
public void onRequest()
{
ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(getResourceStream(),getFileName());
handler.setContentDisposition(ContentDisposition.ATTACHMENT);
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(handler);
}
/**
* Override this method for a file name which will let the browser prompt with a save/open dialog.
* @see ResourceStreamRequestTarget#getFileName()
*/
protected String getFileName()
{
return null;
}
/**
* Hook method providing the actual resource stream.
*/
protected abstract IResourceStream getResourceStream();
}
The (submit)link and behavior implementation:
final AJAXDownload download = new AJAXDownload()
{
@Override
protected IResourceStream getResourceStream()
{
return createResourceStream(item.getModelObject());
}
};
item.add(download);
item.add(new AjaxSubmitLink("link") {
@Override
public void onSubmit(AjaxRequestTarget target, Form<?> form)
{
// do whatever with the target, e.g. refresh components
target.add(...);
// finally initiate the download
download.initiate(target);
}
});
@magomi created a similar implementation in response to this question.
-
Thanks for posting your answer! Please note that you should post the essential parts of the answer here, on this site, or your post risks being deleted [See the FAQ where it mentions answers that are 'barely more than a link'.](http://stackoverflow.com/faq#deletion) You may still include the link if you wish, but only as a 'reference'. The answer should stand on its own without needing the link. – Taryn Jul 19 '13 at 11:29