0

当用户从我的 html5 前端保存一些数据时,我正在尝试创建一个进度更新模块。我有以下代码,但它似乎不起作用。我只是没有得到任何百分比完成值。任何想法为什么这可能会失败。

webservices 是 c# asmx 服务。

$.ajax({
    xhr: function()
    {
        if (window.ActiveXObject) {
            return new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        else {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener("progress", function (evt) {
                if (evt.lengthComputable) {
                    var percentComplete = evt.loaded / evt.total;
                    //Do something with upload progress
                    console.log(percentComplete);
                    alert(percentComplete);
                }
            }, false);

            xhr.addEventListener("progress", function (evt) {
                if (evt.lengthComputable) {
                    var percentComplete = evt.loaded / evt.total;
                    console.log(percentComplete);
                    alert(percentComplete);
                }
            }, false);
            return xhr;
        }
    },
    type: "POST",
    url: "../service.asmx/SaveSession",
    cache: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(DTO),
    dataType: "json",
    async:false,
    success: function (response) {
        result = response;
        IsDataDirty = false;
    },
});
4

1 回答 1

1

使用下面的代码:

   {
    xhr: function() {
      var xhr = $.ajaxSettings.xhr();
      if (xhr.upload) {
      xhr.upload.addEventListener('progress', function(e) {
         var percentage = event.loaded / event.total * 100;
              //.....
         }, false);
      }
       return xhr;
      });
    }
于 2013-08-02T23:22:52.940 回答