angularjs - 我怎样才能得到 content-length header

angularjs - how i can get the content-length header

我正在向具有 xml 文件的 url 发出 http GET 请求,我需要得到响应 header 'Content-Lenght'? 有没有办法得到它?我必须先验证文件的大小才能下载它。

这是我的代码

$http({
     method : "GET",
     url : "http://url/file.xml"
  }).then(function mySucces(data) {

    console.log(response);
    $scope.content = response.data;

  }, function myError(error) {

    console.log(error);
    $scope.content = error.statusText;
  });

来自 Angular 文档,https://docs.angularjs.org/api/ng/service/$http

响应回调得到

  • 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.

  • statusText – {string} – HTTP status text of the response.

您可以通过使用 header 名称调用 headers 函数来获得响应 headers,在本例中为 headers('Content-Type')

$http({
  method: "GET",
  url: "http://b31fe90a.ngrok.io/xml/XML-Meli.xml"
}).then(function mySucces(response) {

  console.log(response);
  console.log(response.headers('content-type'); // it can be `Content-Type` not sure. but can be any header key.
  $scope.content = response.data;

}, function myError(error) {

  console.log(error);
  $scope.content = error.statusText;
});