1

I want to check the file size of the file selected by user, at the client side by using javascript.The code i am using for this is:

<script type="text/javascript">
 var myFile = document.getElementById('myfile');

  //binds to onchange event of the input field
  myFile.addEventListener('change', function() {
  //this.files[0].size gets the size of your file.
   alert(this.files[0].size);

   });
 </script>

But when i run the code, choose a file, nothing happpens. Any body tell me what i am doing wrong

3 Answers 3

3

Your code works fine (in HTML5 browsers with the File API). Make sure that your <script> block is after the <input> element. In that jsfiddle, it's in the "load" handler.

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

2 Comments

or in window.onload=function { ... }
@mplungjan yes I updated the answer a little bit; I got the "onload" for free via jsfiddle :-)
0

Works for me using jquery:

http://jsfiddle.net/UUdcy/

$('#myfile').change( function() {
    var fileInput = $("#myfile")[0];
    var imgbytes = fileInput.files[0].fileSize; // Size returned in bytes.
    $('body').append('<p>'+imgbytes+'</p>');
});​

Comments

0

You can't get the file size using javascript. Security in the browser prevents file system access by Javascript. You'd need to use Flash, or ActiveX or something that would be able to be granted permissions to do this I believe.

EDIT: If you are using the HTML5 File API then I guess you can do this - but as you've not indicated that anywhere I didn't assume that this was the case. I will put HTML as a tag on your post.

1 Comment

He's trying to use the HTML 5 file API I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.