6

我想知道是否有可能,通过使用 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"/>

如您所见,我有 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"/>

在上面添加了 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>

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

4

3 回答 3

12

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

fileLimit="1"

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

“超出最大文件数”

于 2013-10-09T06:47:44.907 回答
8

multiple="false"唯一告诉网络浏览器在特定于浏览器的浏览对话框中禁用多个文件选择。但是,它确实不会阻止最终用户多次单击 PrimeFaces 文件上传部分的选择按钮来多次浏览和添加单个文件。

您最好的选择是引入一些 JS/jQuery 以在选择新文件时删除所有以前选择的文件。如果您已经给出<p:fileUpload>了 a 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();
            }
        }
    });
});

在 PrimeFaces 3.5 上为我工作。

于 2013-02-14T19:39:48.057 回答
0

如果您将文件限制设置为 1 并且在文件加载时发生了一些错误 - 您必须刷新页面才能再次进行文件上传工作。如果您不刷新页面,则会收到超出限制的错误消息。

我使用 JS 解决方案,就像在接受的答案中一样,但必须更改选择器,因为 wigetWar 对我不起作用。

在我看来,我有:

<p:fileUpload id="objectUpload"... />

在我的 portlet 主题中,带有文件的表具有“ui-fileupload-files”的 css 类。

$(document).ready(function() {
  $("div[id*='objectUpload']").find('input[type=file]').change(function() {

    if (this.value) {
        var files = $("div[id*='objectUpload']").find('.uifileupload-files tr');        
        if (files.length > 1) {     
            files.get(0).remove();
        }
    }
  });
});

希望它有所帮助。我使用了 PrimeFaces 6.0

于 2017-01-11T13:29:49.470 回答