【问题标题】:Pass value of another input component when uploading file via p:fileUpload mode=advanced通过 p:fileUpload mode=advanced 上传文件时传递另一个输入组件的值
【发布时间】:2025-12-04 17:55:01
【问题描述】:

我有一个添加 primefaces 文件上传。

<h:form id="form" enctype="multipart/form-data">    
<h:selectOneMenu id="Collection" value="#{imputData.collList.currentColl}">
<f:selectItems value="#{imputData.collList.collList}" />
</h:selectOneMenu>      
<p:fileUpload id="fileUpload" label="Durchsuchen" uploadLabel="Upload" cancelLabel="zurück" fileUploadListener="#{imputData.handleFileUpload}" mode="advanced" dragDropSupport="true" update="Collection,messages" sizeLimit="100000" allowTypes="/(\.|\/)(txt|cvs)$/" />  

 <p:growl id="messages" showDetail="true"/>             
<h:commandButton id="read" action="#{imputData.imput}" value="#{msgs.read}"/>

</h:form>

看起来像这样

我有两个问题。 我怎样才能使文件上传更小。意思是不超过孔页宽?

要从 selectOneMenu 中获取名称,我必须点击阅读按钮 (Lesen)。如果我点击上传按钮,我不会从 SelectOneMenu (null) 获得任何信息。我必须在&lt;p:fileUpload.. 中写 ajax="true" 还是从上传按钮中的 selectOneMenu 获取信息?

我在 fileUpload update="Collection,messages" 的更新中写入了集合中的 ID,但这并没有解决我的问题。

【问题讨论】:

    标签: jsf file-upload jsf-2 primefaces


    【解决方案1】:

    您可以使用fileUpload 的onstart,它会调用remoteCommand 来处理您的selectOneMenu 并将其保存为同一bean 的属性,该bean 必须是视图范围的。

    <p:fileUpload id="fileUpload" mode="advanced" onstart="submitCollection()" />
    
    <p:remoteCommand name="submitCollection" process="@this Collection" />
    

    至于fileUpload的宽度,可以使用如下css:

    .ui-fileupload {
       width: 400px;//change the px into what fits you
    }
    

    希望这会有所帮助。

    【讨论】: