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?
jtcyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jrm213jrm213Connect With a Mentor Commented:
Hi,

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.
0
 
jrm213jrm213Commented:
oh the value <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> is file size in bytes I believe.
0
 
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
0
The new generation of project management tools

With monday.com’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.

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

<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
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");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="form1">
<INPUT TYPE="file" NAME="TxtFile">
<INPUT TYPE="button" NAME="ButCheck" VALUE="SIZE" onClick="check();">
</FORM>
</BODY>
</HTML>
0
 
jrm213jrm213Commented:
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?
0
 
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
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
All Courses

From novice to tech pro — start learning today.