【问题标题】:How limit RichFaces 4 rich:fileUpload to single file upload?如何将 RichFaces 4 rich:fileUpload 限制为单个文件上传?
【发布时间】:2026-01-14 07:40:01
【问题描述】:

我正在使用来自 RichFaces4 的 <rich:fileUpload />,如您所见 here。 但正如你所见,用户可以选择很多图片,但我希望他只能选择一张图片。

上传完成后如何调用托管 bean 中的方法(将图像发送到我的 Amazon S3 存储桶)?

编辑:

<ui:composition>
<h:outputStylesheet>
    .top {
        vertical-align: top;
    }

    .info {
        height: 202px;
        overflow: auto;
    }

    .rf-fu-lst {
        height: 60px;
    }

    .rf-fu-itm {
        border: 0;
    }
</h:outputStylesheet>
<h:outputScript target="head">
jQuery.extend(RichFaces.ui.FileUpload.prototype, {
    __updateButtons: function() {
        if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) {
            if (this.items.length) {
                this.uploadButton.css("display", "inline-block");
                this.addButton.hide();
            } else {
                this.uploadButton.hide();
                this.addButton.css("display", "inline-block");
            }
        } else {
            this.uploadButton.hide();
            this.addButton.css("display", "inline-block");
        }
    }
});
</h:outputScript> 

    <h:form id="form_user_upload_picture" >
        <h:panelGrid columns="2" columnClasses="top, top">
            <rich:fileUpload  
                id="upload" 
                fileUploadListener="#{user_file_upload.listener}"
                acceptedTypes="jpg, gif, png, bmp" 
                addLabel="Adicionar" 
                clearAllLabel="Limpar todas" 
                clearLabel="limpar" 
                deleteLabel="apagar" 
                doneLabel="upload realizado" 
                sizeExceededLabel="arquivo muito grande, tente um arquivo de tamanho menor" 
                serverErrorLabel="ocorreu um erro em nosso servidor, tente novamente por favor"
                uploadLabel="Enviar">

                <a4j:ajax event="uploadcomplete" execute="@none" render="picture" />
            </rich:fileUpload>
        </h:panelGrid>

    </h:form>
</ui:composition>

【问题讨论】:

    标签: jsf file-upload jsf-2 richfaces


    【解决方案1】:

    这曾经在 RF 3.3 中得到支持。但是为了节省 RF 4.0 迁移的时间,&lt;rich:fileUpload&gt; 得到的关注远远少于应有的关注。目前有很多open tickets可以改进。

    在他们改进之前,你目前最好的办法是引入一些自定义的 jQuery/JS 和 CSS 来实现只选择和上传一个文件的功能需求。

    此脚本通过在已选择文件时隐藏添加按钮来防止最终用户上传多个文件:

    jQuery.extend(RichFaces.ui.FileUpload.prototype, {
        __updateButtons: function() {
            if (!this.loadableItem && this.list.children(".rf-fu-itm").size()) {
                if (this.items.length) {
                    this.uploadButton.css("display", "inline-block");
                    this.addButton.hide();
                } else {
                    this.uploadButton.hide();
                    this.addButton.css("display", "inline-block");
                }
            } else {
                this.uploadButton.hide();
                this.addButton.css("display", "inline-block");
            }
        }
    });
    

    确保上面的 JS 在 RF 默认脚本(已经包括 jQuery)之后加载。您可以通过在正文中添加 &lt;h:outputScript&gt; 来完成此操作,如有必要,您可以使用 target="head" 进行设置。

    这个CSS限制了文件列表的高度,去掉了单项的下边框:

    .rf-fu-lst {
        height: 60px;
    }
    .rf-fu-itm {
        border: 0;
    }
    

    确保上面的 CSS 在 RF 默认样式之后加载。您可以通过在正文中添加&lt;h:outputStylesheet&gt; 来做到这一点(所以不要放在头部)。


    上传完成后如何调用托管 bean 中的方法(将图像发送到我的 Amazon S3 存储桶)?

    只需在附加到fileUploadListener 属性的侦听器方法中完成这项工作。

    <rich:fileUpload fileUploadListener="#{bean.upload}">
    

    【讨论】:

    • 我可以在文件上传完成后上传到 S3,谢谢。但是现在我尝试按照您所说的仅上传一个文件,但给了我一个错误:实体名称必须紧跟实体引用中的“&”。
    • 您不应该将 JavaScript 代码放在 XML 文件中,而应放在 JS 文件中 :) 将 &amp;amp; 替换为 &amp;amp;
    • 有没有办法只显示一个显示 Windows 上传窗口的按钮,而不显示所有面板(其中包含按钮添加)?
    最近更新 更多