0

I have tried a lot on searching but could not find a working script for the limitation of the file size using javascript here below is my code but it is not working

   if ($('#photo').get(0).files.size >1024 ) 
       {

                 alert("Please select pic size less than  1 mb file .");
                 return false;
    }

Where photo is id of input type file

5
  • 3
    files[0].size Commented Aug 2, 2018 at 9:43
  • 1
    size is in bytes. you need to divide it by 1024 to get KB and 1024 again to get MB. Ignoring the syntax error your code validates that the file is < 1KB Commented Aug 2, 2018 at 9:44
  • Liam , I have to divide it or multiply ? Commented Aug 2, 2018 at 9:47
  • 1mB = 1,048,576 bytes or you can write 1024 * 1024 Commented Aug 2, 2018 at 9:49
  • thanks I did not put [0] after files , It worked and thanks Curlas Commented Aug 2, 2018 at 10:05

1 Answer 1

0

There was some mistake Which I have forgot thanks for helping me Here is my updated answer

              if ($('#photo').get(0).files[0].size >1048576 ) {
             alert("Please select pic size less than  1mb file .");
             return false;
                }
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.