File upload progress bar with jQuery

前端 未结 9 1703
野的像风
野的像风 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:46

    I have used the following in my project. you can try too.

    ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function () {
    
        if (ajax.status) {
    
            if (ajax.status == 200 && (ajax.readyState == 4)){
                //To do tasks if any, when upload is completed
            }
        }
    }
    ajax.upload.addEventListener("progress", function (event) {
    
        var percent = (event.loaded / event.total) * 100;
        //**percent** variable can be used for modifying the length of your progress bar.
        console.log(percent);
    
    });
    
    ajax.open("POST", 'your file upload link', true);
    ajax.send(formData);
    //ajax.send is for uploading form data.
    

提交回复
热议问题