HTML:

<asp:FileUpload ID="FileUpload1" runat="server" Width="100%"
 onchange="return checkFileExtension(this);" />

# 將FileUpload元件新增至<body></body>之間

 

JavaScript:

<script language="javascript">
   //傳入的elem必須是檔案的完整路徑
    function checkFileExtension(elem) 
    {
        var filePath = elem.value;
        if (filePath.indexOf('.') == -1) 
        {
            remove(elem);
            alert('不正確的副檔名格式');
            return false;
        }
        var validExtensions = new Array();
        var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
        validExtensions[0] = 'tif';
        validExtensions[1] = 'tiff';
        validExtensions[2] = 'pdf';
        
        for (var i = 0; i < validExtensions.length; i++)
        {
            if (ext == validExtensions[i])
            return true;
        }
        remove(elem);
        alert('不正確的副檔名格式: ' + ext.toUpperCase());
        return false;
    }
 
    function remove(control) 
    { 
        var who = control; 
        who.value = ""; 
        var who2 = who.cloneNode(false); 
        who2.onchange = who.onchange; 
        who.parentNode.replaceChild(who2, who); 
    }
</script>
# 將以上JavaScript新增至<head></head>之間
創作者介紹

Frank's Blog

Frank 發表在 痞客邦 PIXNET 留言(0) 人氣()