Primefaces上传,如何只允许一次上传模式

use*_*929 6 jsf file-upload primefaces

我想知道是否可能,通过使用primefaces上传提前模式来限制用户仅上传一个文件,目前我有:

 <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                                  mode="advanced" 
                                  multiple="false" 
                                  update="messages"
                                  sizeLimit="100000000" 
                                  allowTypes="/(\.|\/)(gif|jpe?g|png|doc|docx|txt|pdf)$/"
                                  auto="false"/>


                    <p:growl id="messages" showDetail="true"/>
Run Code Online (Sandbox Code Playgroud)

你可以看到我有muliple ="false",但用户仍然可以上传多个文件,任何提示?

编辑:

                <p:fileUpload widgetVar="upload" fileUploadListener="#{fileUploadController.handleFileUpload}"
                              mode="advanced" 
                              multiple="false" 
                              update="messages"
                              label="Select File"
                              sizeLimit="100000000" 
                              allowTypes="/(\.|\/)(gif|jpe?g|png|doc|docx|txt|pdf|html)$/"
                              auto="false"/>


                <p:growl id="messages" showDetail="true"/>
Run Code Online (Sandbox Code Playgroud)

已添加上面的widgetVar

在我的js

<script type="text/javascript"> 
        function Naviagtion()
        {
            //alert("Sent to the printing holding queue, you may close this app now, your work will still print out ");
            window.setTimeout(afterDelay, 500);
            location.href = 'FilesUploaded.xhtml';

        }

        upload.buttonBar.find('input[type=file]').change(function() {
            if (this.value) {
                var files = upload.uploadContent.find('.files tr');

                if (files.length > 1) {
                    files.get(0).remove();
                }
            }
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

但我仍然可以多上传,我正朝着正确的方向前进

cpz*_*cpz 12

虽然解决它的更好的行为应该像@BalusC建议的那样,但在primefaces 4.0中我看到了属性

fileLimit="1"
Run Code Online (Sandbox Code Playgroud)

您可以设置为1以禁止使用"选择"按钮添加多个文件.当用户添加更多文件时,它只是说

"超出最大文件数"


Bal*_*usC 8

multiple="false"只告诉web浏览器在浏览器特定的禁用选择多个文件浏览对话框.但是,它确实不会阻止最终用户在PrimeFaces文件上载部分的"选择"按钮上多次单击以多次浏览和添加单个文件.

最好的办法是在选择新文件时引入一些JS/jQuery来删除所有以前选择的文件.只要你给你的<p:fileUpload>一个widgetVar="upload",那么这应该这样做:

$(document).ready(function() {
    upload.buttonBar.find('input[type=file]').change(function() {
        if (this.value) {
            var files = upload.uploadContent.find('.files tr');

            if (files.length > 1) {
                files.get(0).remove();
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

适用于PrimeFaces 3.5.