1. 程式人生 > >獲取 input[type=file] 檔案上傳尺寸

獲取 input[type=file] 檔案上傳尺寸

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <input type="file" name="f" id="f" />  
            <input type="button" name="aa" id="aa" value="測試" onclick="javascript: _s();" />  
        </div>  
    </form>  
</body>  
</html>  
<script type="text/javascript">  
    function _s() {  
        var f = document.getElementById("f").files;  
        //上次修改時間  
        alert(f[0].lastModifiedDate);  
        //名稱  
        alert(f[0].name);  
        //大小 位元組  
        alert(f[0].size);  
        //型別  
        alert(f[0].type);  
    }  
</script>


判斷檔案型別
var type=(src.substr(src.lastIndexOf("."))).toLowerCase();
  if(type!=".jpg"&&type!=".gif"&&type!=".jpeg"&& type!=".png"){
  alert("您上傳圖片的型別不符合(.jpg|.jpeg|.gif|.png)!");
  return false;
}