Upload a file AJAX-ically using Wicket
-
22-07-2019 - |
Question
I want to upload a file with AJAX in Wicket. It seems to me that Wicket does not support this feature. Is it possible?
Solution
Check out the source code from the Wicket Examples for upload: http://www.wicketstuff.org/wicket13/upload/single. It has examples for both standard and ajax versions.
OTHER TIPS
As an update to an old question, it seems it is possible right now:
This approach works for me with a full Ajax wicket app. Sorry, this is Scala syntax but should be easily transferable back to Java syntax:
import java.io.File
import org.apache.wicket.markup.html.form.upload.FileUploadField
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.WebPage
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink
import org.apache.wicket.ajax.AjaxRequestTarget
class TestPage extends WebPage {
val uploadForm = new Form("form")
val fileField = new FileUploadField("file")
uploadForm.add(fileField)
add(form)
add(new AjaxSubmitLink("submit", uploadForm) {
def onSubmit(target: AjaxRequestTarget, form: Form[_]) {
val upload = fileField.getFileUpload
if (upload != null) {
val file: File = upload.writeToTempFile
}
}
})
}
HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<body>
<form wicket:id="form">
<input wicket:id="file" type="file"/>
</form>
<button wicket:id="submit">Upload</button>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow