jsf中的ajax化文件上传

标签 ajax jsf file-upload

我想上传文件而不发布整个表单。文件上传工作正常,但提交了整个表单。当验证正确时,这可以正常工作。但是当体育运动时必填字段为空,上传失败并返回错误信息(必填字段缺失) 所以我尝试使用ajax上传文件(ajax=true)。但上传之后什么也没做。

我尝试了一种解决方法,将文件上传和其他字段以不同的形式放置。这可行,但结果是在执行文件上传时,您在其他字段中更改的数据将被忽略。

有什么建议吗?

这是我使用的代码:

<t:inputFileUpload id="fileupload" value="#{prospectDetail.upFile}" size="50" />
<h:outputLabel for="description" value="#{msg.prospectdetail_description}"/>
<mw:inputText id="description" size="40" value="#{prospectDetail.fileDescription}" />
<p:commandButton styleClass="button" value="#{msg.common_upload}" action="#{prospectDetail.upload}" ajax="false" process="@form" onbegin="busyPopup.show()" oncomplete="busyPopup.hide();"/>

最佳答案

XMLHttpRequest 的第一个版本无法上传文件(这是 JavaScript 中核心的 Ajax 请求 Controller 对象)。第二版XMLHttpRequest支持它,但这并没有被 <p:commandButton> 实现(目前浏览器支持率较低)。

既然您似乎已经在使用 PrimeFaces,为什么不直接使用它自己的 <p:fileUpload> 成分? single upload甚至是auto upload示例应该可以为您完成此操作(添加 PrimeFaces 的文件上传过滤器后,不要忘记从 web.xml 中删除 MyFaces 扩展过滤器!)。 PrimeFaces 的<p:fileUpload>只要可用,就会自动利用 XHR2 文件上传。

I tried a work around bu putting the file upload and other fields in different forms. This works, but the result is that data you changed in the other fields is disregarded when doing the file upload.

如果将bean放入 View 范围而不是请求范围并返回nullvoid从操作方法,那么这应该可以工作。

关于jsf中的ajax化文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015773/

相关文章:

java - 无法在 JSF 中链接到 CSS 中的图像

php - 为什么以下代码在 PHP 中上传文件时会抛出 undefined index ?

html - 文件上传(仅限相机/非图库)

php - 通过 AJAX 获取用户特定数据

php - 创建一个jquery函数,根据比较将分数添加到MySQL表中的total-points列中

ajax - 当要阻止的组件通过 AJAX 更新时,PrimeFaces 阻止 UI 不起作用

jsf - Datatable 不对 primefaces 中的元素进行排序

node.js - 使用 req.files 上传文件不适用于 Express 4.x

php - 如何在 PALM webOS 中连接到外部数据库(例如 MySQL)

jquery - Bootstrap 弹出窗口 : reload content with ajax