最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Get file size in javascript for multiple files - Stack Overflow

programmeradmin8浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论