How can you determine the file size in JavaScript?

As you know IE supports the fileSize property of an image. No such luck in other browsers ... however, you should be able to modify this script:

http://natbat.net/2008/Aug/27/addSizes/

It uses JSON to read HTTP headers of files and display their actual file size. That should help you prevent people uploading large animated GIFs.

As for getting the dimensions:

var img = new Image();
theImage.src = "someimage.jpg";
actualwidth = theImage.width;
actualheight = theImage.height;

This of course is a pure client-side approach to something best handled server-side.


Actually, with HTML5, this is now possible,
read more information here.


Short answer, you cannot

Also, Check on jGuru How can you check the file size from JavaScript in a form with an input type of file?

You will find some important points

Well the answer is very simple, you cannot.

Reason: The browser security does not allow the scripts (Javascript/VBScript) or even applets and ActiveX Controls to read files from the local hard disk. You can only read files if your code is signed by some Certificate Authority (CA). Now the input type "FILE" also does not have the permission to read files. We cannot do anything about that since thats what HTML says. So since it cannot read files, it cannot find the size of the file with which the input tag is associated. Since it cannot find the file size, there is no function exposed by JavaScript/VBScript to return the file size. But if you need to find the file size, say in order to restrict the size of the file uploaded to your web-server. Then you can do so by counting the file contents on the server-side, once the user submits it to the server. Thats what many of the free e-mail providers like www.hotmail.com do.