duanchi5078 2015-07-17 04:37
浏览 56
已采纳

我可以使用JavaScript每次向服务器发送三个请求吗?

The problem: I'm uploading images via JavaScript to a asp.net server page. To compute the progressive operation for each image uploading, I designed that each image has an independent request. But I'm afraid this may overload the server, specially if the number of requests is big.

The idea: Rather than sending all requests asynchronously I want to send them as packets of three.
After those three are sent and all other requests have to wait until those three finished.

The Question:

How i can make the the other requests to wait until the previous ones finish?

The code:

for (var i = 0; i < inputFiles.files.length; i++) {
    (function (i) {                
        var request = new XMLHttpRequest();

        request.open('POST', 'Ajax.ashx');
        request.setRequestHeader('Cashe-Control', 'no-cashe');

        var data = new FormData();
        data.append('file[]', inputFiles.files[i]);

        request.upload.addEventListener('progress', function (event) {//happening
            if (event.lengthComputable) {
                var percent = parseFloat(event.loaded) / parseFloat(event.total), 
                    progressWidth = percent * parseFloat(progressBarWidth);
                progressBar.children().css('width', progressWidth + 'px');
            } else {}
        });
        request.upload.addEventListener('load', function (event) {});
        request.upload.addEventListener('error', function (event) {});
        request.addEventListener('readystatechange', function (event) {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    var code = eval(this.response);
                } else {}
            }
        });
        request.send(data);
    })(i);
}
  • 写回答

2条回答 默认 最新

  • dsfhd78978 2015-07-17 05:37
    关注

    Because you've added the jQuery tag, you could:

    • create n $.ajax() requests
    • wait with the help of $.when() until all of them resolved
    • and then send the next n requests

    Something like this:

    function uploadImages(images) {
        var requests = [];
    
        // upload 3 images (or less, if there are less images in the array)
        for (var i = 0, len = Math.min(3, images.length); i < len; i++) {
            var image = images.pop(),
                formData = new FormData(),
                request, formData = new FormData();
    
            formData.append('file[]', image);
    
            request = $.ajax({
                url: "Ajax.ashx",
                type: "POST",
                data: formData,
                processData: false,
                beforeSend: function (xhr) {
                    // add progress stuff
                    // http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/
                },
                success: function (data) { }
            });
    
            requests.push(request);
        }
    
        // when all our requests have finished and there are images left, start the next round...
        $.when.apply($, requests).done(function () {
            if (images.length) {
                uploadImages(images);
            }
        });
    }
    

    Simplified Example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料