zoukankan      html  css  js  c++  java
  • [javascript] 用 js 检测上传文件大小

    原文地址: http://www.dotblogs.com.tw/cross/archive/2010/09/21/17840.aspx

    google了很久,基本上都是用 activeX 来实现~至于为什么不行,这个不多说,说一下以下方法:

    已经测试通过的浏览器:IE6+,firefox,chrome,其中 firefox 和 chrome 要能支持 HTML5。

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=big5">
        <title>上传</title>
    </head>
    <body>
        <form action="XXXXXX" method="POST" name="FileForm" enctype="multipart/form-data">
        <div align="center">
            图片:
            <input type="file" name="file1" size="20" id="file1" />
            <input type="button" onclick="checkFile()" /></div>
        </form>
    </body>
    </html> 
    <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>

    <script language="JavaScript" type="text/javascript">
        var fileSize = 0; //文件大小
        var SizeLimit = 1024;  //上传上限,单位:byte

        function checkFile() {
            var f = document.getElementById("file1");
            //FOR IE
            if ($.browser.msie) {
                var img = new Image();
                img.onload = checkSize;
                img.src = f.value;
            }
            //FOR Firefox,Chrome
            else {
                fileSize = f.files.item(0).size;
                checkSize();
            }
        }

        //检查文件大小
        function checkSize() {
            //FOR IE FIX
            if ($.browser.msie) {
                fileSize = this.fileSize;
            }

            if (fileSize > SizeLimit) {
                alert('文件超过大小');
            } else {
                document.FileForm.submit();
            }
        }
    </script>
  • 相关阅读:
    Codeforces 1065C Make It Equal
    Codeforces 1065B Vasya and Isolated Vertices
    Codeforces 1065A Vasya and Chocolate
    Luogu P2467 [SDOI2010]地精部落
    Codeforces 1042C Array Product
    Codeforces 1041C Coffee Break
    JMeter安装和环境变量搭建
    Jenkins
    Jenkins介绍
    Docker
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/2373467.html
Copyright © 2011-2022 走看看