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

1
  • Do we really need angularjs mention in tag, body and title? This appears to be pretty much a generic JS question Commented Dec 16, 2017 at 19:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.