JavaScript: Checking file size before accepting upload

Posted on 2007-10-03
Last Modified: 2008-01-09
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?
Question by:jtcy
    LVL 17

    Accepted Solution


    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.
    LVL 17

    Expert Comment

    oh the value <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> is file size in bytes I believe.
    LVL 75

    Assisted Solution

    by:Michel Plungjan
    It does not work in any browsers as far as I know...
    It is advisory and php can read this value I believe
    LVL 6

    Expert Comment

    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.

    <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");
    <FORM NAME="form1">
    <INPUT TYPE="file" NAME="TxtFile">
    <INPUT TYPE="button" NAME="ButCheck" VALUE="SIZE" onClick="check();">
    LVL 17

    Expert Comment

    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?
    LVL 75

    Expert Comment

    by:Michel Plungjan
    Either ActiveX or Java would have to be allowed to access the local file system.
    Lost of popups to click OK to
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now