package org.apache.wicket.examples.ajax.builtin;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.Bytes;
import org.apache.wicket.validation.validator.StringValidator;
@author
public class FileUploadPage extends BasePage
{
private static final long serialVersionUID = 1L;
private final FileUploadField file;
private final TextField<String> text;
public FileUploadPage()
{
final Component feedback = new FeedbackPanel("feedback").setOutputMarkupId(true);
add(feedback);
final Form<?> form = new Form<Void>("form")
{
private static final long serialVersionUID = 1L;
@see
@Override
protected void onSubmit()
{
info("Text: " + text.getModelObject());
FileUpload upload = file.getFileUpload();
if (upload == null)
{
info("No file uploaded");
}
else
{
info("File-Name: " + upload.getClientFileName() + " File-Size: " +
Bytes.bytes(upload.getSize()).toString());
}
}
};
form.setMaxSize(Bytes.megabytes(1));
add(form);
form.add(text = new TextField<>("text", new Model<String>()));
text.add(StringValidator.minimumLength(2));
form.add(file = new FileUploadField("file"));
form.add(new Label("max", new AbstractReadOnlyModel<String>()
{
private static final long serialVersionUID = 1L;
@Override
public String getObject()
{
return form.getMaxSize().toString();
}
}));
form.add(new UploadProgressBar("progress", form, file));
form.add(new AjaxButton("ajaxSubmit")
{
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form)
{
info("This request was processed using AJAX");
target.add(feedback);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form)
{
target.add(feedback);
}
});
}
}