2

Here is my code:

    var image = $('#image_gms');
    console.log(image[0].files.length);
    console.log(image[0].files.size);

And result:

1
undefiend

So files.length returns me 1 (there is an image) but cannot get the size, i'm following second answer from this question.

Sahin Urkin
  • 289
  • 1
  • 8

2 Answers2

2

Apparently, image[0].files is something that has length === 1, I'm assuming an array or an array-like object. Therefore, to get the file's size, do

image[0].files[0].size
mbojko
  • 13,503
  • 1
  • 16
  • 26
  • When you say, "I'm assuming an array". That's wrong. `image[0].files` return a `FileList` object. You can get the size of an `File` object with the property `size`. – R3tep Sep 17 '19 at 08:56
1

image[0].files return a FileList. The property length return the number of File into the list.
If you want the size of a file, you can use the property size directly on a File object.

Like :

image[0].files[0].size

// or

const sizes = []
for (let l = image[0].files.length; l--;) {
  var file = files.item(l);
  sizes.push(file.size)
}
console.log(sizes)
R3tep
  • 12,512
  • 10
  • 48
  • 75