Get size of file requested via ajax

前端 未结 3 1378
心在旅途
心在旅途 2020-12-03 07:54

Here\'s what I\'m hoping to do:

I want to send an ajax request to a file (preferably with jQuery), and once the file has been loaded, determine the size of the reque

3条回答
  •  心在旅途
    2020-12-03 08:54

    You could make a HTTP HEAD request, and get a file size approximate by reading the Content-Length HTTP Header.

    This kind of request is used to obtain meta-information about the URL implied by the request, without transferring any content of it in the response.

    var xhr = $.ajax({
      type: "HEAD",
      url: "path/to/file.ext",
      success: function(msg){
        alert(xhr.getResponseHeader('Content-Length') + ' bytes');
      }
    });
    

提交回复
热议问题