Search code examples
plupload

How to limit Total files size in PLUpload


I am using PLUpload UI Widget and below is the code

$(function() {
    $("#uploader").plupload({
        runtimes : 'html5,flash,silverlight,html4',
        url : "/examples/upload",
        max_file_size : '20mb', 
        chunk_size: '1mb',
 
        resize : {
            width : 200,
            height : 200,
            quality : 90,
            crop: true // crop to exact dimensions
        },
 
        filters : [
            {title : "Image files", extensions : "jpg,gif,png"}
        ],
 
        rename: true,       
        sortable: true,
        dragdrop: true,
        views: {
            list: true,
            thumbs: true, // Show thumbs
            active: 'thumbs'
        },
       flash_swf_url : '/plupload/js/Moxie.swf',   
        silverlight_xap_url : '/plupload/js/Moxie.xap'
    });
});

I want to allow user to upload files which should not exceed 18mb together. So now if any one selects a file greater than 15mb or less than 18mb this code works good but when user choose 2 files of 15mb-4mb than total is 19 mb and this will not show any error.

I saw few solutions for QueueWidget(plqueueupload) but did not find any solutions for UIWidget (plupload)

There are built in options to limit a single file's size and also number of files. How can we limit the size of all the files together ?


Solution

  • I found answer after going through the PUUpload documentation, adding this just to help if anyone else is looking for this.

    "Selected" event invoked after we select every file and in that we can validate.

    selected: function(uploader, files) {
        if(files.files.length > 0) {
           for(var i=0;i<files.files.length;i++) {
               maxsize += files.files[i].size;
           }
           if(maxsize > 18874368) {
              //add your logic to show error message
              $("#fileUploader").plupload("notify", "error", "Files size exceeded 18MB");
          }
    }