I have a simple Wicket form that allows users to select some data and then upload a zip file (generated on the fly) containing what they ask. Currently, the form method onSubmit()looks something like this:
public void onSubmit() {
IResourceStream stream = ;
ResourceStreamRequestTarget target = new ResourceStreamRequestTarget(stream);
target.setFileName("download.zip");
RequestCycle.get().setRequestTarget(target);
}
This works, but of course the request stops there and it is not possible to display any other feedback for the user.
What I would like to have is something like a typical "Your requested [NAME] download should start automatically. If not, click this link." Ideally, the same page is still displayed, so the user can immediately select some other data and load it.
I assume that this can be done using the Wicket Ajax classes, but I managed to avoid using them so far, and it does not immediately seem to me right away. What is my fastest way out here?
Updated for the answer from Zeratul, below: I got something like this:
class MyDownloader extends AbstractAjaxBehavior {
private final MyForm form;
MyDownloader(MyForm form) {
this.form = form;
}
void startDownload(AjaxRequestTarget target) {
target.addComponent(myForm);
target.appendJavascript("window.location.href='" + getCallbackUrl() + "'");
}
@Override
public void onRequest() {
try {
ResourceStreamRequestTarget streamTarget = form.getStreamTarget();
form.info();
getComponent().getRequestCycle().setRequestTarget(streamTarget);
catch (SomeException e) {
form.error(e.getMessage());
}
}
}
class MyForm extends Form {
private final MyDownloader myDownloader;
private final Object myModel;
MyForm(Object aModel) {
super("myForm");
myModel = aModel;
myDownloader = new MyDownloader(this);
add(myDownloader);
add();
add(new AjaxButton("download", new Model<String>("Download"), this) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
myDownloader.startDownload(target);
}
});
add(new FeedbackPanel("feedback"));
}
ResourceStreamRequestTarget getStreamTarget() throws SomeException {
return ;
}
}
This is a little annoying, but it seems to work.
source
share