Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

jquery - Get size of file requested via ajax

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 requested file.

After a bit of googling, it's clear that I don't even have a good idea of the right question to ask to figure this out. Any help would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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');
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...