日期:2014-05-16  浏览次数:20348 次

控制上传图片大小问题

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript"> 
        var ImgObj = new Image(),FileObj;
        
        function CheckProperty(obj){
            var AllowImgFileSize = 0,ImgFileSize=0;
            
            FileObj = obj;
            
            AllowImgFileSize = FileObj.getAttribute("imgSize");
            
            if(ImgObj.readyState != "complete")
            {
                setTimeout("CheckProperty(FileObj)", 500);
                return false;
            }
            ImgFileSize = Math.round(ImgObj.fileSize / 1024 * 100) / 100;
            if(AllowImgFileSize < ImgFileSize)
                alert("图片文件大小超过限制。请上传小于" + AllowImgFileSize + "KB的文件,当前文件大小为" + ImgFileSize + "KB");
        }
        
        function CheckImgSize(obj,imgSize){
            var AllImgExt = ".jpg|.jpeg|.gif|.bmp|.png|";
            
            FileObj = obj;
            
            obj.setAttribute("imgSize",imgSize);
            
            if(obj.value == "")
                return false;
            
            ImgObj.src = obj.value;
            CheckProperty(obj);
        }
    </script> 
</head>
<body>
    <input type="file" name="file1" onchange="CheckImgSize(this,50)" />
    <input type="file" name="file2" onchange="CheckImgSize(this,