JavaScript: Checking file size before accepting upload

I have a file upload bar for people to upload image. I am looking for a script that can pop up a window warning user that the file selected has file over the maximum file size limit. This script has to check the file size before starting to accept file upload. Is there anyway to do it?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

jrm213jrm213Connect With a Mentor Commented:

No. Javascript cannot access the file system, it is one of the safeguards in place so that people don't write a javascript that is able to "harm" or "gather data from" someones computer.

I believe you can do this:

<input type="hidden" name="MAX_FILE_SIZE" value="10000000">

and if you place that right before your <input type="file"> tag it will not submit if the file is too big.

so would be like

<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input type="file" name="myfileinputname"/>

I am not sure if this is cross-browser functionality or only works in some browsers.
oh the value <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> is file size in bytes I believe.
Michel PlungjanConnect With a Mentor IT ExpertCommented:
It does not work in any browsers as far as I know...
It is advisory and php can read this value I believe
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

You have to create an ActiceX...Try this out..
1. Make sure you use IE 5.5 and above
2. Just allow activeX by adding that page as trusted site.

function check()
var Objfile = new ActiveXObject("Scripting.FileSystemObject");
var fileval = document.form1.TxtFile.VALUE;
var fist = objfile.getFile(fileval);
var fsize = fist.size;
alert(fsize + " bytes");
<FORM NAME="form1">
<INPUT TYPE="file" NAME="TxtFile">
<INPUT TYPE="button" NAME="ButCheck" VALUE="SIZE" onClick="check();">
ActiveX won't work in anything but IE as far as I know.

Maybe a Java Applet would provide the functionality needed and work cross browser?
Michel PlungjanIT ExpertCommented:
Either ActiveX or Java would have to be allowed to access the local file system.
Lost of popups to click OK to
Forced accept.

EE Admin
All Courses

From novice to tech pro — start learning today.