Stan
Stan

Reputation: 26501

Get file size in javascript for multiple files

I am using jQuery Form Plugin to upload my files via AJAX and I also want to check if size is more than 20mb on every file before I send them to server. I've discovered this HTML5 example, but I can't figure out how to put it together with my code here.

$(document).ready(function(){
    $('.form-videos').ajaxForm({
        success : function(data){
            alert(data);
        },
        beforeSubmit : function(){
            var fileInput = $('.form-videos :input[type=file]');
            var totalFiles = $('.form-videos :input[type=file]').get(0).files.length;

            for (i = 0; i < totalFiles; i++)
            {
                alert('what?'); // This works and prints out correctly for every file
                // How do I get current file size?
            }
        }
    });
});

Upvotes: 2

Views: 12607

Answers (2)

Amadan
Amadan

Reputation: 198314

See an example here.

In your example, length is simply the number of selected files. You need to access the individual File objects and their size property.

Upvotes: 0

Alex K.
Alex K.

Reputation: 175758

You should be able to;

var files = $('.form-videos :input[type=file]').get(0).files;
for (i = 0; i < files.length; i++)
{
   if (files[i].size > 20971520) ...
}

Upvotes: 5

Related Questions