Progress bar while uploading large files with XMLHttpRequest

后端 未结 2 1565
孤独总比滥情好
孤独总比滥情好 2020-12-14 20:31

I am trying to upload some large files to the server using XMLHttpRequest and file.slice.
I\'ve manage doing this with the help of documentations and other various li

相关标签:
2条回答
  • 2020-12-14 20:35

    There is my solution:

    function addImages(id) {
      var files = $("#files").prop("files");
      var file = files[loopGallery];
      var cList = files.length;
    
      var fd = new FormData();
      fd.append("file", file);
      fd.append("galerie", id);
    
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "moduls/galerie/uploadimages.php", true);
      xhr.upload.onprogress = function(e) {
        var percentComplete = Math.ceil((e.loaded / e.total) * 100);
        $("#progress").css("display","");
        $("#progressText").text((loopGallery+1)+" z "+cList);
        $("#progressBar").css("width",percentComplete+"%");
      };
    
      xhr.onload = function() {
        if(this.status == 200) {
          $("#progressObsah").load("moduls/galerie/showimages.php?ids="+id);
          if((loopGallery+1) == cList) {
            loopGallery = 0;
          } else {
            $("#progressBar").css("width", "0%");  
            loopGallery++;
            addImages(id);
          }
        }
      }
    
      if(cList > 0) {
        xhr.send(fd);
      }
    }
    
    0 讨论(0)
  • 2020-12-14 20:50

    As stated in https://stackoverflow.com/a/3694435/460368, you could do :

    if(xhr.upload)
         xhr.upload.onprogress=upload.updateProgress;
    

    and

    updateProgress: function updateProgress(evt) 
    {
       if (evt.lengthComputable) {
           var progress = Math.ceil(((upload.loaded + evt.loaded) / upload.total) * 100);
           $("#dvProgressPrcent").html(progress + "%");
           $get('dvProgress').style.width = progress + '%';
       }
    }
    
    0 讨论(0)
提交回复
热议问题