5

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

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

2 回答 2

1

使用 data.files.length

 $('#fileupload').bind('fileuploadsubmit', function (e, data) {
    var fileCount = data.files.length;
    if (fileCount < filestoupload) {
        upload = false; 
    }
});
于 2012-07-17T13:35:31.263 回答
1

这对我有用,在您的文件上传定义上添加一个 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; 
        }
    }
  });

到目前为止,抛出并不优雅,如果您碰巧实现了这一点并找到了避免这种情况的方法,请告诉我(现在是必要的,否则它将为每个上传的文件显示错误警报)

于 2012-08-28T23:12:16.680 回答