Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 使用$http访问原始XHR对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要访问原始 XMLhttprequest对象,以在@L_489_1@它的浏览器上添加文件上载进度回调.这是可能的,还是我必须自己构建原始请求?如果是这样,我如何在promise对象中包装原始XMLhttprequest?
我模拟了构建自定义XMLhttprequest的$http调用,如下所示:
uploadFile(file,progressHandler) {
  var xhr = new XMLhttprequest(),deferred = $q.defer();

  xhr.open("POST","your/path",truE); // method,url,async
  xhr.setrequestHeader("Content-Type",file.type || "application/octet-stream");
  xhr.onreadystatechange = function (E) {
    if (xhr.readyState == 4) {
      $rootScope.$apply(function () {
        // Construct a response object SIMILAR TO a regular $http call
        //
        // data – {String|Object} – The response body transformed with the transform functions.
        // status – {number} – http status code of the response.
        // headers – {function([headerName])} – Header getter function.
        // config{Object} – The configuration object that was used to generate the request.
        var r = {
          data: xhr.response,status: xhr.status,headers: xhr.getResponseHeader,config: {}
        };
        if (r.status == 200) {
          deferred.resolve(r);
        } else {
          deferred.reject(r);
        }
      });
    }
  };
  if (progressHandler && xhr.upload) {
    xhr.uplo@R_607_10198@ddEventListener('progress',function(E) {
      progressHandler((e.loaded / e.@R_379_10586@l),E);
    },falsE);
  }
  // This is only available in XHR2,provide multipart fallBACk
  // if necessary
  xhr.send(filE);

  return deferred.promise;
}

大佬总结

以上是大佬教程为你收集整理的angularjs – 使用$http访问原始XHR对象全部内容,希望文章能够帮你解决angularjs – 使用$http访问原始XHR对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。