File upload progress bar with jQuery

前端 未结 9 1784
野的像风
野的像风 2020-11-22 08:52

I am trying to implement an AJAX file upload feature in my project. I am using jQuery for this; my code submits the data using AJAX. I also want to implement a file upload p

9条回答
  •  时光说笑
    2020-11-22 09:51

    Kathir's answer is great as he solves that problem with just jQuery. I just wanted to make some additions to his answer to work his code with a beautiful HTML progress bar:

    $.ajax({
    xhr: function() {
    var xhr = new window.XMLHttpRequest();
    
    xhr.upload.addEventListener("progress", function(evt) {
      if (evt.lengthComputable) {
        var percentComplete = evt.loaded / evt.total;
        percentComplete = parseInt(percentComplete * 100);
    
        $('.progress-bar').width(percentComplete+'%');
        $('.progress-bar').html(percentComplete+'%');
    
      }
    }, false);
    
    return xhr;
    },
    url: posturlfile,
    type: "POST",
    data: JSON.stringify(fileuploaddata),
    contentType: "application/json",
    dataType: "json",
    success: function(result) {
    console.log(result);
    }
    });
    

    Here is the HTML code of progress bar, I used Bootstrap 3 for the progress bar element:

    
    

提交回复
热议问题