user1559811
user1559811

Reputation: 449

determine total file size when using multipe file select

I have some file requirement conditions which work fine with single file select but when using multiple file select it only counts the first file. I need it to determine the total sum of bites of all selected files and that all selected files have the accepted file extension. does anyone know how I can edit these conditions?

var file = document.getElementById('file'); 
var ext = file.value.substring(file.value.lastIndexOf('.') + 1);

   if(ext!== "mp4" && ext!== "m4v" && ext!== "fv4"){
       alert('not an accepted file extension');
         return false;
} else

   if (file.files[0].size >= 11000000) {
       alert("File too Big");
          return false; 
}

  <input name="uploaded[]"  id="file" type="file" multiple />

Upvotes: 0

Views: 189

Answers (1)

pimvdb
pimvdb

Reputation: 154948

You can map the .files to their sizes, and then add those sizes: http://jsfiddle.net/83F7B/.

var sizes = [].map.call(this.files, function(v) {
  return v.size;
});

var totalSize = sizes.reduce(function(a, b) {
  return a + b;
}, 0);

You can also loop over the .files with forEach and check the extension of each file (or use .every).

Upvotes: 1

Related Questions