如何在上传前验证最大文件数?

时间:2012-07-16 22:59:34

标签: javascript jquery jquery-plugins

您好我正在使用上传文件插件,我需要在上传文件之前验证添加的文件数量......这样的事情

        $('#fileupload').bind('fileuploadadd', function (e, data) {
        filestoupload++;
        var numOfDivs = $('.request').size();
        if (numOfDivs < filestoupload) {
            upload = false; // Is just an example.
        }
    });

2 个答案:

答案 0 :(得分:1)

使用data.files.length

 $('#fileupload').bind('fileuploadsubmit', function (e, data) {
    var fileCount = data.files.length;
    if (fileCount < filestoupload) {
        upload = false; 
    }
});

答案 1 :(得分:1)

这对我有用,在你的fileupload定义中添加一个beforesend,然后进行验证

 var maxfiles=3;
 $('#fileupload').fileupload(({
    url: postFileUrl,
    submit: function (event, files) {
        //check for max files THIS IS WHERE YOU VALIDATE
        //console.log(files.originalFiles.length);
        var fileCount = files.originalFiles.length;
        if (fileCount > maxFiles) {
            alert("The max number of files is "+maxFiles);
            throw 'This is not an error. This is just to abort javascript';
            return false; 
        }
    }
  });
到目前为止,抛出是不优雅的,如果你正在实现这个并找到一种方法来避免这种情况请告诉我(现在是必要的,否则它将显示上传的每个文件的错误警报)

相关问题