Javascript to check filesize before upload in Internet Explorer

后端 未结 4 1198
别那么骄傲
别那么骄傲 2020-12-17 16:54

Is it possible to use Javascript to check for a file\'s size (at the client side) before it is actually uploaded to the server?

The application is built on EXTJS and

相关标签:
4条回答
  • 2020-12-17 17:31

    It is possible with ActiveX Objects.

    <html>
    <head>
    <script>
    function getSize()
    {
        var myFSO = new ActiveXObject("Scripting.FileSystemObject");
        var filepath = document.upload.file.value;
        var thefile = myFSO.getFile(filepath);
        var size = thefile.size;
        alert(size + " bytes");
    }
    </script>
    </head>
    <body>
    <form name="upload">
    <input type="file" name="file">
    <input type="button" value="Size?" onClick="getSize();">
    </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-17 17:36

    There's currently no way to portably check the size of an uploaded file from the web browser. The HTML5 File API makes this possible, but that's not available in MSIE7 -- it's currently on track for MSIE10.

    There is intentionally no way to determine the full path of an uploaded file, as that may include sensitive information, like the name of the end user.

    0 讨论(0)
  • 2020-12-17 17:49

    Please try below code,

    <html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript">
    $(function() {
    $("document").ready(function(){
            $("#myFile1").change(function() {
    
            var f1=document.getElementById("myFile1").value;
            if((f.size||f.fileSize)==09765625)
            {
            alert("file size is less than 1mb");
            }
            else
            {
            alert("file size should not exceed more than 1mb");
                    $(this).val($.data(this, 'f'));
            return false;
            }
    
                });
    });
    })
    </script>
    </head>
    <body>
    
    <input type='file' name="file" id="myFile1" />
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-17 17:51

    The reason you can't is because it would be bad if browsers, and javascript, could access the clients filesystem.

    This is actively denied and can be seen as an attack.

    Using jQuery, Restricting File Size Before Uploading

    0 讨论(0)
提交回复
热议问题