View Full Version : Image file size

12-10-2010, 09:42 PM
I have a form that allows photos to be uploaded. I am using Perl to do this, and it works fine. Now I want to validate that the size of the image does not exceed a certain limit BEFORE it actually uploads. I can do that in Perl using something like the code below. But, I would rather do this in Javascript. I already have a routine that validates that the image file name does not contain embedded spaces. I would like to add this there.

Can this be done in Javascript?

if ($ENV{'CONTENT_LENGTH'} > 100000) { print "SIZE EXCEEDS LIMIT!" }

Old Pedant
12-10-2010, 09:56 PM
JavaScript has no ability to manipulate files in the client's computer. And, yes, that even includes reading the directory to find whether a file exists and/or finding the size of the file.

You *CAN* do this in MSIE (only) by using an ActiveX object, but even with MSIE this is considered "unsafe scripting" and is only allowed if your site is a "trusted domain" (as specified by the browser user, not by you!) *AND* if the user allows the code to run. In short, even with MSIE it's not a viable alternative except for maybe inTRAnet situations.

12-11-2010, 01:38 PM
And, yes, that even includes reading the directory to find whether a file exists
Not really. In fact JavaScript can sense if an external file exists or not, but only after the document tries to load it.

<img src="moo/boo.jpg" onerror="alert('no such a folder/file found!');this.parentNode.removeChild(this)">

It works in all the browsers I know (including IE6 :D). Except that in Opera, for a reason or another, the event fires twice.

But, indeed, JavaScript is not able to find the size of a ready for upload file.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum