xhr.upload.onprogress doesn't work

前端 未结 1 1970
我寻月下人不归
我寻月下人不归 2021-01-02 15:11

Everything in the following code will work, except it will never fire the xhr.upload.onprogress event.

$(function(){

    var xhr;

    $(\"#submit\").click(         


        
相关标签:
1条回答
  • 2021-01-02 15:42

    You should create the listeners before opening the connection, like this:

    $(function(){
    
        var xhr;
    
        $("#submit").click(function(){
            var formData = new FormData();
            formData.append("myFile", document.getElementById("myFileField").files[0]);
            xhr = new XMLHttpRequest();
    
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4 && xhr.status === 200){
                    console.log(xhr.responseText);              
                }
            }
    
            xhr.upload.onprogress = function(e) {
               // it will never come inside here
            }
    
            xhr.open("POST", "./test.php", true);
            xhr.send(formData);
        });
    });
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题