Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Determine file size using Javascript

    Is it possible to use Javascript to determine the size of a file before it's actually uploaded to the server using a multipart form?

  • #2
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope, you can't because client side scripts cannot have access to files on the hard drive.

    You could do it with ActiveX but highly recommend you don't do that.

    Why not just check the filesize with a server side script?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mark87
    Why not just check the filesize with a server side script?
    Because them I have to upload the entire file to the server before I can check the size.

    So if you have a slow connection it'll take several minutes to upload the file, and then it's a pain in the neck to have to wait several minutes to get the message back from the server that the file is too large.

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    If you use an file upload component, it will tell you if the file size is too big without the whole file being fully uploaded.

    There are many server-side file upload components on the market whcih enable easy saving and manipulation of files. Simply search on google for "file upload component" and whatever server-side language you are using (php/asp/etc).

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Client side solution:

    Code:
    function ShowFileSize(filespec)
    {
       var fso, f, s;
       fso = new ActiveXObject("Scripting.FileSystemObject");
       f = fso.GetFile(filespec);
       s = f.Name + " uses " + f.size + " bytes.";
       return(s);
    }
    This would obviously require some low security settings on the client or an .HTA file.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    That code will only work in browsers that support JScript (ie. IE) and don't have ActiveX disabled (ie. have ignored Microsoft's security suggestions).

    The code will give an error if you try to run it as Javascript since Javascript doesn't recognise the ActiveX call - you should test if ActiveX is recognised first to determine whether the browser supports Javascript or JScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    See if this code helps

    <html>
    <head>
    <script language="JavaScript">
    function A()
    {
    var oas = new ActiveXObject("Scripting.FileSystemObject");
    var d = document.a.b.value;
    var e = oas.getFile(d);
    var f = e.size;
    var mb=f/(1024);
    alert(mb + "kilo bytes");
    }
    </script>
    </head>
    <body>
    <form name="a">
    <input type="file" name="b">
    <input type="button" name="c" value="SIZE" onClick="A();">
    </form>
    </body>
    </html>

  • #8
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <html>
    <head>
    <script language="JavaScript">
    function A()
    {
    var oas = new ActiveXObject("Scripting.FileSystemObject");
    var d = document.a.b.value;
    var e = oas.getFile(d);
    var f = e.size;
    alert(f + " bytes");
    }
    </script>
    </head>
    <body>
    <form name="a">
    <input type="file" name="b">
    <input type="button" name="c" value="SIZE" onClick="A();">
    </form>
    </body>
    </html>

    code for file size in bytes


  •  

    Posting Permissions

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