its happening because you are using input type="submit" for the cancel button, which of course submits the form. you should instead use a link for cancel buttons. you can attach a link to a input type="button" if you want cancel to still be a button btw...
-igor On Thu, Feb 21, 2008 at 5:56 PM, Dan Kaplan <[EMAIL PROTECTED]> wrote: > Hello, > > > > I've made an upload form and wanted to add a cancel button to it. The > cancel button is clicked if the user decides he doesn't want to upload > (before he uploads) and should redirect back to another page. This works > pretty well except I noticed that if the user chooses a file then clicks the > cancel button, the file starts uploading anyway! Here's the code that does > this. I can't see why it's happening: > > > > > > import org.apache.wicket.authorization.strategies.role.Roles; > > import > org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstant > iation; > > import > org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar; > > import org.apache.wicket.extensions.wizard.CancelButton; > > import org.apache.wicket.markup.html.WebPage; > > import org.apache.wicket.markup.html.form.Form; > > import org.apache.wicket.markup.html.form.TextField; > > import org.apache.wicket.markup.html.form.Button; > > 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.PropertyModel; > > import org.apache.wicket.util.file.Files; > > import org.apache.wicket.util.file.Folder; > > > > import java.io.File; > > > > @AuthorizeInstantiation(Roles.USER) > > public class CreateAReport extends WebPage { > > > > private String reportName; > > > > public CreateAReport() { > > final FileUploadForm form = new FileUploadForm("form"); > > final UploadProgressBar progressBar = new > UploadProgressBar("progress", form); > > > > form.add(progressBar); > > form.add(new TextField("name", new PropertyModel(this, > "reportName")).setRequired(true)); > > > > Button submit = new Button("submitbutton") { > > public void onSubmit() { > > form.upload(); > > setResponsePage(CustomReport.class); > > } > > }; > > form.add(submit); > > > > Button cancel = new Button("cancelbutton") { > > public void onSubmit() { > > setResponsePage(ReportList.class); > > } > > }; > > cancel.setDefaultFormProcessing(false); > > form.add(cancel); > > > > add(form); > > > > add(new FeedbackPanel("feedback")); > > } > > > > public String getReportName() { > > return reportName; > > } > > > > public void setReportName(String reportName) { > > this.reportName = reportName; > > } > > > > private class FileUploadForm extends Form { > > protected FileUploadField fileUploadField; > > protected File newFile; > > > > /** > > * Construct. > > * > > * @param name Component name > > */ > > public FileUploadForm(String name) { > > super(name); > > > > // set this form to multipart mode (allways needed for uploads!) > > setMultiPart(true); > > > > // Add one file input field > > add(fileUploadField = new FileUploadField("fileInput")); > > fileUploadField.setRequired(true); > > > > // Set maximum size to 100K for demo purposes > > // setMaxSize(Bytes.kilobytes(100)); > > } > > > > public void upload() { > > final FileUpload upload = fileUploadField.getFileUpload(); > > if (upload != null) { > > // Create a new file > > newFile = new File(getUploadFolder(), > upload.getClientFileName()); > > > > // Check new file, delete if it allready existed > > checkFileExists(newFile); > > try { > > // Save to new file > > newFile.createNewFile(); > > upload.writeTo(newFile); > > } catch (Exception e) { > > throw new IllegalStateException("Unable to write file: " > + newFile.getAbsolutePath()); > > } > > } > > } > > > > public File getFile() { > > return newFile; > > } > > } > > > > private void checkFileExists(File newFile) { > > if (newFile.exists()) { > > // Try to delete the file > > if (!Files.remove(newFile)) { > > throw new IllegalStateException("Unable to overwrite " + > newFile.getAbsolutePath()); > > } > > } > > } > > > > private Folder getUploadFolder() { > > Folder uploadFolder = new > Folder(System.getProperty("java.io.tmpdir"), "wicket-uploads"); > > // Ensure folder exists > > uploadFolder.mkdirs(); > > return uploadFolder; > > } > > > > } > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]