Topic: Limit Max Uploads

Hello,

I'm trying to limit the number of uploads to 5 using FilesAdded and FilesRemoved events.

At first you can only add 5 files. (using max_file_count)

When I add a single or less than 5 files I set the max_file_count = max_file_count - files.lenght of the queue, but it makes no difference, when i try to add files it come back to original max_file_count of 5.

What do I do?

heres the code:

..

$(function() {
    $("#uploader").plupload({
        runtimes : 'flash,html5,browserplus,silverlight,gears,html4',
        url : 'plupload/upload.php',
        max_file_count: 5,
        unique_names : true,
        multiple_queues : true,
        rename: true,
        drop_element: 'uploader',
        sortable: true,
        filters : [
            {title : "Arquivos de Imagens (jpg, gif, png)", extensions : "jpg,gif,png"}
        ],
        flash_swf_url : 'plupload/js/plupload.flash.swf',
        silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
        init : {
            FilesAdded: function(up, files) {
                if (files.length >= 5) {
                    $('#uploader_browse').hide("slow"); //if greater than 5, hides the browse button
                } else {
                    up.settings.max_file_count = up.settings.max_file_count - files.length;
                    alert(up.settings.max_file_count); // Shows the max_file_count for test
                }               
            },
            FilesRemoved: function(up, files) {
                if (up.files.length <= 5) {
                    $('#uploader_browse').fadeIn("slow");
                }
            }
        }
    });
});

Thanks in advance.

2 (edited by kadu2c15 2011-03-18 20:31:17)

Re: Limit Max Uploads

Fix it!!!

.....

var maxfiles = 5; ////edit here the number of max uploads
$(function() {
    $("#uploader").plupload({
        runtimes : 'flash',
        url : 'plupload/upload.php',
        //max_file_size : '1000mb',
        max_file_count: maxfiles,
        //chunk_size : '1mb', //divide o arkivo em partes de 1mb
        unique_names : true,
        multiple_queues : true,
        multi_selection: true,
        rename: true,
        drop_element: 'uploader',
        sortable: true,
        filters : [
            {title : "Arquivos de Imagens (jpg, gif, png)", extensions : "jpg,gif,png"}
        ],
        flash_swf_url : 'plupload/js/plupload.flash.swf',
        silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
       
        init : {
            FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    if (up.files.length > maxfiles) {
                        up.removeFile(file);
                    }
                    var upa = $('#uploader').plupload('getUploader');
                    var i = 0;
                    while (i<=upa.files.length) {
                        ultimo = upa.files.length;
                        if (ultimo > 1) {
                            if (i > 0) {
                                ultimo2 = ultimo - 1;
                                ii = i-1;
                                if (ultimo2 != ii) {
                                    if (up.files[ultimo - 1].name == upa.files[i-1].name) {
                                        up.removeFile(file);
                                    }
                                }
                            }
                        }
                        i++;
                    }
                });
                if (up.files.length >= maxfiles) {
                    $('#uploader_browse').hide("slow");
                }
            },
            FilesRemoved: function(up, files) {
                if (up.files.length < maxfiles) {
                    $('#uploader_browse').fadeIn("slow");
                }
            }
        }
    });
   
    $('form').submit(function(e) {
        var uploader = $('#uploader').plupload('getUploader');

        if (uploader.total.uploaded == 0) {
            if (uploader.files.length > 0) {
                uploader.bind('UploadProgress', function() {
                    if (uploader.total.uploaded == uploader.files.length)
                        $('form').submit();
                });
                uploader.start();
            } else
                alert('You must at least upload one file.');
            e.preventDefault();
        }
    });
});

....

<style>
#uploader_start {
    z-index:999999;
}
</style>

Feel free to use smile



EDITED:

Now with anti-duplications smile

Re: Limit Max Uploads

Hello kadu2c15,

Thank you for sharing code, I have been looking for a way to limit the number of files that can be uploaded for some times. I integrated your code and it is limiting the number of files that can be uploaded, but I get an error message that prevents the ‘Add file’ button from being hidden.

I would greatly appreciate your help in tying to resolve this.

This is the error message that I get from the console:

In chrome:
Uncaught TypeError: Object [object Object] has no method 'plupload'
(anonymous function) in :8888/…/:74
g.each in: plupload.full.min.js:2
$.pluploadQueue.init.FilesAdded in :8888/…/:70
g.Uploader.g.extend.trigger in plupload.full.min.js:2
l in plupload.full.min.js:2
f.runtimes.Html5.f.addRuntime.init.m.bind.A.onchange in plupload.full.min.js:2

In Firefox:
$("#uploader").plupload is not a function
var upa = $('#uploader').plupload('getUploader');

Thank you

Re: Limit Max Uploads

Hello kadu2c15,

I found why it was not working.

I am using pluploadQueue and you are using plupload.

Could you point me in the right direction to update the code in order to make it work with pluploadQueue?

Thank you

Re: Limit Max Uploads

i am trying to add this code to html scricpt but could not. need urgent solution

Re: Limit Max Uploads

Hi travel,

I managed to make this work on my website, so maybe I can help.

The first thing that you have to make sure is that you are using plupload (and not pluploadQueue)

$(function() {
    $("#uploader").plupload({...

Let us know what is your issue.

Re: Limit Max Uploads

Hello,

Sorry for the delay...
..

Sorry, but i can't help with your problem, i just made the plugin to plupload (not pluploadqueue),

Because it didn't had a way to limit I did a logic myself...

The logic is the same just make the code to Pluploadqueue..

or use plupload....

here is the entire code of the working page...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Teste - Plupload</title>


<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/smoothness/jquery-ui.css" type="text/css" />
<link rel="stylesheet" href="plupload/css/jquery.ui.plupload.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="plupload/js/gears_init.js"></script>
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script type="text/javascript" src="plupload/js/plupload.full.min.js"></script>
<script type="text/javascript" src="plupload/js/jquery.ui.plupload.min.js"></script>
<script type="text/javascript" src="plupload/js/jquery.plupload.queue.min.js"></script>
<script type="text/javascript" src="plupload/src/javascript/i18n/pt-br.js"></script>
<script type="text/javascript">
var maxfiles = 5; //edit here the number of max uploads
$(function() {
    $("#uploader").plupload({
        runtimes : 'flash',
        url : 'plupload/upload.php',
        //max_file_size : '1000mb',
        max_file_count: maxfiles,
        //chunk_size : '1mb', //divide o arkivo em partes de 1mb
        unique_names : true,
        multiple_queues : true,
        multi_selection: true,
        rename: true,
        drop_element: 'uploader',
        sortable: true,
        filters : [
            {title : "Arquivos de Imagens (jpg, gif, png)", extensions : "jpg,gif,png"}
        ],
        flash_swf_url : 'plupload/js/plupload.flash.swf',
        silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
       
        init : {
            FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    if (up.files.length > maxfiles) {
                        up.removeFile(file);
                    }
                    var upa = $('#uploader').plupload('getUploader');
                    var i = 0;
                    while (i<=upa.files.length) {
                        ultimo = upa.files.length;
                        if (ultimo > 1) {
                            if (i > 0) {
                                ultimo2 = ultimo - 1;
                                ii = i-1;
                                if (ultimo2 != ii) {
                                    if (up.files[ultimo - 1].name == upa.files[i-1].name) {
                                        up.removeFile(file);
                                    }
                                }
                            }
                        }
                        i++;
                    }
                });
                if (up.files.length >= maxfiles) {
                    $('#uploader_browse').hide("slow");
                }
            },
            FilesRemoved: function(up, files) {
                if (up.files.length < maxfiles) {
                    $('#uploader_browse').fadeIn("slow");
                }
            }
        }
    });
   
    $('form').submit(function(e) {
        var uploader = $('#uploader').plupload('getUploader');

        if (uploader.total.uploaded == 0) {
            if (uploader.files.length > 0) {
                uploader.bind('UploadProgress', function() {
                    if (uploader.total.uploaded == uploader.files.length)
                        $('form').submit();
                });
                uploader.start();
            } else
                alert('You must at least upload one file.');
            e.preventDefault();
        }
    });
});
</script>
</head>
<body>

<h1>MultiUpload</h1>

<form method="post" action="sendimages.php">
    <div id="uploader" style="width:500px;">
        <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
    </div>
    <input type="hidden" value="action" name="hid" /><br />
    <input type="submit" name="enviaf" value="Enviar" />
</form>

</body>
</html>

i'ts in portuguese-brazil (where i'm from)..

hope i helped... bye

Re: Limit Max Uploads

Hello kadu2c15,

I converted my code to plupload and managed to get it to work.
Once more, thank you very much for sharing this with us!

9 (edited by masimao 2011-09-21 19:20:17)

Re: Limit Max Uploads

Hi,

I am using a solution to limit the number of files to upload which has a small code. Here is:


// Maximum Queue
var queueMaxima = 2;

uploader.bind('FilesAdded', function(up, files) {


    // Check if the size of the queue is bigger than queueMaxima
    if(up.files.length > queueMaxima)
    {

        // Removing the extra files
        while(up.files.length > queueMaxima)
        {
            if(up.files.length > queueMaxima)
                uploader.removeFile(up.files[queueMaxima]);
        }

        alert("Max " + queueMaxima + " files.");

    }

});

As kadu2c15, i am from Brazil too, so the variables are in Portuguese.

I hope this can help!

Re: Limit Max Uploads

See this
http://www.plupload.com/punbb/viewtopic.php?pid=5100

Re: Limit Max Uploads

masimao wrote:

Hi,

I am using a solution to limit the number of files to upload which has a small code. Here is:


// Maximum Queue
var queueMaxima = 2;

uploader.bind('FilesAdded', function(up, files) {


    // Check if the size of the queue is bigger than queueMaxima
    if(up.files.length > queueMaxima)
    {

        // Removing the extra files
        while(up.files.length > queueMaxima)
        {
            if(up.files.length > queueMaxima)
                uploader.removeFile(up.files[queueMaxima]);
        }

        alert("Max " + queueMaxima + " files.");

    }

});

As kadu2c15, i am from Brazil too, so the variables are in Portuguese.

I hope this can help!


my code  seeks for duplicated files too...

Re: Limit Max Uploads

hi,
nice post thanks for sharing such a nice post.

Re: Limit Max Uploads

Is this 5 uploads per person?

Re: Limit Max Uploads

this is very fine.