I see that FileUpload is available now via Ajax
(https://issues.apache.org/jira/browse/WICKET-2420). However, is it possible
to upload from a ModalWindow via Ajax?
In my AjaxButton, my FileUpload object is not being set. I am also not seeing
an IFrame in the Wicket Debug
I have attached my code if that may help.
Thanks
- Doug
package com.alllocal.wicket.main.newBusiness.photos;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
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.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import com.alllocal.model.AllLocalBinaryContent;
import com.alllocal.model.business.BusinessLocation;
import com.alllocal.model.business.BusinessPhoto;
import com.positiontech.common.model.StoredImage;
import com.positiontech.common.wicket.BasePanel;
import com.positiontech.common.wicket.PTFeedbackPanel;
public class NewPhotoPanel extends BasePanel {
private BusinessPhoto photo;
private IModel<BusinessLocation> location;
private FileUploadField fileUploadField;
private FileUpload fileUpload;
private FeedbackPanel feedback;
private WebMarkupContainer dataContainer;
public NewPhotoPanel(String id, IModel<BusinessLocation> inLocation) {
super(id);
this.location = inLocation;
photo = new BusinessPhoto();
photo.setContent(new AllLocalBinaryContent());
Form form = new Form("photoForm");
add(form);
form.setMultiPart(true);
dataContainer = new WebMarkupContainer("dataContainer");
form.add(dataContainer);
dataContainer.setOutputMarkupId(true);
feedback = new PTFeedbackPanel("feedback");
dataContainer.add(feedback);
feedback.setOutputMarkupId(true);
form.setDefaultModel(new CompoundPropertyModel(photo));
fileUploadField = new FileUploadField("content.fileName",
new PropertyModel(this, "fileUpload"));
addFormComponentWithFeedback(dataContainer, fileUploadField);
// addFormComponentWithFeedback(dataContainer, new
RequiredTextField(
// "title"));
form.add(new AjaxButton("addPhoto") {
@Override
protected void onSubmit(AjaxRequestTarget target,
Form<?> form) {
try {
AllLocalBinaryContent media =
photo.getContent();
if (fileUpload != null) {
String fileName =
fileUpload.getClientFileName();
String contentType =
fileUpload.getContentType();
byte[] content =
fileUpload.getBytes();
if
(StoredImage.isImageAllowed(contentType) == false) {
error("File type is not
supported. "
+
"Supported types are JPEG, GIF, and PNG.");
return;
}
// TODO do we want to massage
the image to a certain
// size if
// is too big
media.setFileName(fileName);
media.setContentType(contentType);
media.setContent(content);
location.getObject().getPhotos().add(photo);
ModalWindow.closeCurrent(target);
} else {
error("Unable to upload file.");
target.addComponent(feedback);
}
} catch (Exception e) {
error(e);
log(e);
target.addComponent(feedback);
}
}
@Override
protected void onError(AjaxRequestTarget target,
Form<?> form) {
target.addComponent(dataContainer);
}
});
form.add(new AjaxLink("cancel") {
@Override
public void onClick(AjaxRequestTarget target) {
ModalWindow.closeCurrent(target);
}
});
}
public FileUpload getFileUpload() {
return fileUpload;
}
public void setFileUpload(FileUpload fileUpload) {
this.fileUpload = fileUpload;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]