发送文件的顺序

时间:2017-04-17 22:25:49

标签: javascript ajax

我不知道该怎么做,只有两个文件同时发送到服务器,就像这里UploadImages一样。在我的代码中,所有文件都是同时发送的。这是我的代码。填写帖子需要的话。填写帖子所需的词语。

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 --- */
            for (var i = 0; i < files.length; i++) {
                uploadFile(files[i], percent_info[i], p_bar[i]);
            }
            
            function uploadFile(file, percent_info, p_bar) {
            var url = "server/index.php";


            if (file.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.style.width = percentLoaded + "%";
                            
                        }
                    }
                });
         
                xhr.upload.addEventListener("load", function(e) {
                        var percentLoaded = Math.round((e.loaded / e.total) * 100);
                    percent_info.style.width = percentLoaded + "%";
                    
                });


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

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

0 个答案:

没有答案