jQuery's “uploadProgress” not firing in “$.ajax”

后端 未结 1 966
醉酒成梦
醉酒成梦 2021-01-03 06:01

I am new to jQuery and now, I am currently working on file uploads. And I want to add some progress bar each time I upload image. I used the uploadProgress in

相关标签:
1条回答
  • 2021-01-03 06:25

    According to the $.ajax() reference, uploadProgress is not a valid option.

    Instead, the xhr option is used instead, which lets you set progress listeners on the XMLHttpRequest that is used by the ajax request.

    this answer shows how to do that:

    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener("progress", function(evt) {
                if (evt.lengthComputable) {
                    var percentComplete = (evt.loaded / evt.total) * 100;
                    //Do something with upload progress here
                }
           }, false);
           return xhr;
        },
        type: 'POST',
        url: "/",
        data: {},
        success: function(data){
            //Do something on success
        }
    });
    
    0 讨论(0)
提交回复
热议问题