客户端JS验证fileupload控件,设置只允许特定的文件类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" language="javascript">
      
        function CheckFileType()
        {  
            var objButton=document.getElementById("Button1");//上传按钮
            var objFileUpload=document.getElementById('FileUpload1');//FileUpload
            var objMSG=document.getElementById('msg');//显示提示信息用的DIV
            var FileName=new String(objFileUpload.value);//文件名
            var extension=new String (FileName.substring(FileName.lastIndexOf(".")+1,FileName.length));//文件扩展名
            
            if(extension=="jpg"||extension=="JPG")//你可以添加扩展名
            {
                 objButton.disabled=false;//启用上传按钮
                 objMSG.innerHTML="";
            }
            else
            {
                  objButton.disabled=true;//禁用上传按钮
                  objMSG.innerHTML="请选择正确的文件文件";
             }
        }
        
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="msg"></div>
        <asp:Button ID="Button1" runat="server" Text="上传" Enabled="False" />
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" onChange="javascript:CheckFileType();" />
    
    </div>
    </form>
</body>
</html>

你可能感兴趣的:(fileupload)