Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces upload, how to only allow one upload in advance mode

I am wondering if it possible, by using the primefaces upload in advance mode to limit the user uploading one file only, currently i have :

 <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"/>

as you can see i have muliple ="false" but a user is still able to upload multiple files, any tips ?

EDIT :

                <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"/>

have added the widgetVar above

and in my 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>

but i am still able to multi upload, am i going about this in the right direction

like image 324
user2065929 Avatar asked Feb 14 '13 17:02

user2065929


2 Answers

Although better behavior to solve it should be as @BalusC suggested, but in primefaces 4.0 I am seeing the attribute

fileLimit="1"

which you can set to 1 to disallow multiple file additions using "Choose" button. When user adds more file then it simply says

"Maximum number of files exceeded"

like image 160
cpz Avatar answered Oct 06 '22 00:10

cpz


The multiple="false" only tells the webbrowser to disable multiple file selection in the browser-specific Browse dialog. However, it indeed doesn't prevent the enduser from clicking multiple times on the Choose button of the PrimeFaces file upload section to browse and add a single file multiple times.

Your best bet is to bring in some JS/jQuery to remove all previously selected files when a new file is selected. Provided that you have given your <p:fileUpload> a widgetVar="upload", then this should do:

$(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();
            }
        }
    });
});

Works for me on PrimeFaces 3.5.

like image 28
BalusC Avatar answered Oct 06 '22 00:10

BalusC