SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Sep 2004
    Location
    India
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Way to determin filesize before uploading ?

    Hi,

    I have a file upload script for my website and i need to check the filesize before its uploaded to the server. Is there anyway i can do it using Javascript on client side so both user/server do not waste their resources ?

    Thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Javascript can't see the file and server side processing can't access it until after it is uploaded. The best you can do before that is to limit the file size so that files above a certain limit can't be uploaded.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member
    Join Date
    Sep 2004
    Location
    India
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    That is what i am doing right now...and this is what i am trying to avoid. User first uploads files which takes a bit of time and then after waiting they are told that the file they uploaded is big...and user gets mad.

    I thought maybe there was some sort of technique to avoid this problem or something.

    Thanks.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's possible to do this with an Applet if you're prepared to go that route. Google for "file upload applet" for more info.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can check this using an ActiveX object
    Code:
    <html>
    <head>
    <title>Check size</title>
    <script type="text/javascript">
    function checkSize(oForm)
    {
    	var oFileSystem = new ActiveXObject("Scripting.FileSystemObject");
    	var sFileName = oForm["fileName"].value;
    	var oFile = oFileSystem.getFile(sFileName);
    	var iSize = oFile.size;
    
    	alert("File size is " + iSize + " bytes");
    }
    
    </script>
    </head>
    <body>
    
    <form name="myForm">
    	<input type="file" name="fileName">
    	<input type="button" value="Check size" onclick="checkSize(this.form);">
    </form>
    
    </body>
    </html>
    Note thought that this is IE Windows only

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since you can't rely on your visitor having ActiveX or an applet installed you will still need the server side code to check anyway.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, ActiveX is basically evil. And starting from version 7 of IE its turned off by default. I would only recommend using it in Intranets for example


Bookmarks

Posting Permissions

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