使用 Wicket 以 AJAX 方式上传文件

发布于 2024-07-30 06:47:11 字数 60 浏览 4 评论 0原文

我想在 Wicket 中使用 AJAX 上传文件。 在我看来,Wicket 不支持此功能。 是否可以?

I want to upload a file with AJAX in Wicket. It seems to me that Wicket does not support this feature. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-08-06 06:47:11

查看 Wicket 示例中的源代码进行上传: http://www.wicketstuff.org/ wicket13/上传/单。 它有标准版本和 ajax 版本的示例。

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.

故事↓在人 2024-08-06 06:47:11

作为对旧问题的更新,现在似乎是可能的:

http:// /www.wicket-library.com/wicket-examples/ajax/upload

As an update to an old question, it seems it is possible right now:

http://www.wicket-library.com/wicket-examples/ajax/upload

Oo萌小芽oO 2024-08-06 06:47:11

这种方法对我来说适用于完整的 Ajax 检票口应用程序。
抱歉,这是 Scala 语法,但应该可以轻松转换回 Java 语法:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文