Check File Size using JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Check File Size using JavaScript

    Hi there!

    I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript.

    I have seen suggested solutions using an ActiveX control to check the file size; however, I'm not happy with this solution because my application is designed to work in multiple browsers and ActiveX is limited to Internet Explorer.

    I cannot check the file size on the web server because IIS throws the following exception before my code is executed:
    "System.Web.Htt pException: Maximum request length exceeded"

    I realize that the maximum request length can be increased, however according to my application's specifications the files uploaded (images) must be much less than 4MB in size (200K).

    Any suggestions or recommendations about how to check the file size using JavaScript are welcome.

    Thanks,

    -Frinny
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I think that what I'm asking is impossible.
    I have recently come across an explanation as to why:

    Browser security does not allow Scripts to read files from the local hard drive. This means that the input type "File" doesn't have permissions to read files. Therefore, there is no way to use JavaScript to check the file size.



    -Frinny

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      you can do it in firefox3; file inputs provide a getAsBinary method.

      the length of the resulting string is your filesize.

      IE can use the activeX stuff.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Do you know of any solutions for Opera and Safari as well?

        I've started looking into a server side fix for this so that it can work in any browser. The main ones I test with are IE, FireFox, Opera and Safari.

        Comment

        Working...