使用AjaxUpload Codeigniter上传时如何计算所选文件的数量?

时间:2016-05-06 06:13:02

标签: javascript php ajax codeigniter

我想计算选择上传的文件数量,现在它显示一个文件名,而不是要显示选择上传的文件数。

我在Codegniter中使用AjaxUpload上传文件。

JS代码:

$(function () {
    var inputFile = $('input#file');
    var uploadURI = $('#form-upload').attr('action');
$('#upload-btn').on('click', function(event) {
        var filesToUpload = inputFile[0].files;
if (filesToUpload.length > 0) {
for (var i = 0; i < filesToUpload.length; i++) {
                var file = filesToUpload[i];
                formData.append("file[]", file, file.name);             
            }

$.ajax({
                url: uploadURI,
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                success: function(data) {
                    console.log(data);
                    listFilesOnServer();
                },
                xhr: function() {
                    var xhr = new XMLHttpRequest();
                    xhr.upload.addEventListener("progress", function(event) {
                        if (event.lengthComputable) {
                            var percentComplete = Math.round( (event.loaded / event.total) * 100 );
                            $('.progress').show();
                            progressBar.css({width: percentComplete + "%"});
                            progressBar.text(percentComplete + '%');
                        };
                    }, false);

                    return xhr;
                }
            });

1 个答案:

答案 0 :(得分:0)

试试这段代码:

var inputFile = $('#form-upload input:file');
var uploadURI = $('#form-upload').attr('action');
$('#upload-btn').on('click', function(event) {
    for (i=0; i<inputFile.length; ++i) {
      if (inputFile[i].value != ""){
       filesToUpload[i] = inputFile[i].files;
       filesToUpload[i]["name"] = inputFile[i].value;
      }
    }
    /* ... */