在队列中发送两个文件

时间:2017-04-26 16:24:31

标签: javascript ajax

嘿我在queque中发送两个文件时遇到问题。当我选择2个文件时,我不知道如何做到这一点,2个一次上传,接下来的2个在队列中等待。现在当我选择4个文件时,发送2个,接下来的两个不发送。请查看我的code。也许你有个主意。我必须做些什么让它在功能感应中起作用?

(function() {
    var imageType = /image.*/;


    function uploadFile(file, percent_info, p_bar, licznik) {
        var url = "server/index.php";

        if (file[licznik].type.match(imageType)) {
            var xhr = new XMLHttpRequest();
            var fd = new FormData();

            xhr.upload.addEventListener("progress", function(evt) {
                if (evt.lengthComputable) {

                    var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
                    if (percentLoaded < 100) {
                        percent_info[licznik].style.width = percentLoaded + "%";

                    }
                }
            });

            xhr.upload.addEventListener("load", function(e) {
                    var percentLoaded = Math.round((e.loaded / e.total) * 100);
                percent_info[licznik].style.width = percentLoaded + "%";

            });


            function ready() {
                return function() {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        p_bar[licznik].classList.add('trans_completed');

                        if (licznik < file.length){
                            licznik++;
                        }
                    }
                };
            }

            xhr.onreadystatechange = ready();
            xhr.open("POST", url, true);
            fd.append('uploaded_file', file[licznik]);
            xhr.send(fd);
        }
    };



    var uploadfiles = document.querySelector('#file-upload');
    uploadfiles.addEventListener('change', function() {
        var files = this.files;



        var percent_info = document.querySelectorAll('.progress_bar:not(.trans_completed) .percent');
        var p_bar = document.querySelectorAll('.progress_bar:not(.trans_completed)');
        /* --- Upload files to server loop --- */


            (function induction(files, percent_info, p_bar) {
                 counter_file = 0;
                 counter_file_2 = 1;

                 function caller() {                        
                        uploadFile(files, percent_info, p_bar, counter_file);
                        uploadFile(files, percent_info, p_bar, counter_file_2);                        
                 }
                 caller();
            })(files, percent_info, p_bar);

    });

})();

0 个答案:

没有答案