admin管理员组

文章数量:1321070

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?
            }
        }
    });
});

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?
            }
        }
    });
});
Share Improve this question asked Feb 16, 2012 at 17:40 StanStan 26.5k55 gold badges168 silver badges247 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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) ...
}

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.

本文标签: jqueryGet file size in javascript for multiple filesStack Overflow