Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Image file size

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Atlanta, GA
    Thanked 0 Times in 0 Posts

    Image file size

    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!" }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,747 Times in 4,709 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Bucharest, ROMANIA
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Old Pedant View Post
    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 ). 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.
    Last edited by Kor; 12-11-2010 at 02:44 PM.
    Offshore programming


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts