Showing posts with label file_size. Show all posts
Showing posts with label file_size. Show all posts

Sunday, February 3, 2013

Find size of file behind download link with jquery before download from server

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: "http://domain.com/file.ext",
  success: function(msg){
    alert(xhr.getResponseHeader('Content-Length') + ' bytes');
  }
});

Friday, February 1, 2013

get file size by jquery before upload

You actually don't have access to filesystem (for example reading and writing local files), however, due to HTML5 File Api specification, there are some file properties that you do have access to, and the file size is one of them.
For the HTML bellow
<input type="file" id="myFile" />
try the following:
//binds to onchange event of your input field
$('#myFile').bind('change', function() {

  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);

});
As it is a part of the HTML5 specification, it will only work for modern browsers (and IE is not one of them).