我想上传文件而不发布整个表单。文件上传工作正常,但提交了整个表单。当验证正确时,这可以正常工作。但是当体育运动时必填字段为空,上传失败并返回错误信息(必填字段缺失) 所以我尝试使用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 范围而不是请求范围并返回null
或void
从操作方法,那么这应该可以工作。
关于jsf中的ajax化文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015773/