-2

I am trying to implement file uploader in angularJS but with a condition that if input file is empty it should give a massage. I used following code:

if(var inputFileLength=document.getElementById("#id").value==0) 
alert ("input file is empty");

But it is not working. So please give some solution for the same.

georgeawg
  • 48,608
  • 13
  • 72
  • 95

1 Answers1

1

I believe value will get the filename, so that won't work.

We can get details of the selected files using event.target.files, and use the .size property.

Try this:

function selected(event) {
  let file = event.target.files[0];
  if (file.size === 0) {
    alert("Its empty");
  } else {
    alert("size is " + file.size);
  }
 }
<input type="file" onchange="selected(event)" />

This is not supported on IE < 11 though...

https://developer.mozilla.org/en-US/docs/Web/API/File

user184994
  • 17,791
  • 1
  • 46
  • 52