Search code examples
jqueryfine-uploader

How to limit overall file size?


I found a setting that set limit for a single file

validation: {
    sizeLimit: 51200 // 50 kB = 50 * 1024 bytes
}

How to implement validation as follows: any number can be uploaded to server but grand total must be less than 5mb ?


Solution

  • The best way to do this is to make use of the onValidate callback. There, you can keep a running total of all submitted file sizes. Once you reach your max, you can start rejecting files. For example:

    var totalAllowedSize = 5000000,
        totalSizeSoFar = 0;
    
    callbacks: {
        onValidate: function(data) {
            if (totalSizeSoFar + data.size > totalAllowedSize) {
                return false;
            }
            totalSizeSoFar += data.size;
        }
    }