我的页面上有一个 ASP 文件上传控件。如何在客户端验证所选文件。验证规则: 1. 文件必须为 jpeg,png,bmp 或 gif。2. 文件大小必须在 25 kb 和 2 mb 以内。请帮忙。谢谢。
我已尝试以下代码来验证扩展
<asp:FileUpload ID="FileUpload2" runat="server"/>
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateFileUpload" ErrorMessage="Invalid file type. Only .gif, .jpg, .png, .bmp and .jpeg are allowed." ControlToValidate="FileUpload2" ValidationGroup="update"> </asp:CustomValidator>
<script language="javascript" type="text/javascript">
function ValidateFileUpload(Source, args) {
var fuData = document.getElementById('<%= fuData.ClientID %>');
var FileUploadPath = fuData.value;
if (FileUploadPath == '') {
// There is no file selected
args.IsValid = false;
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "jpg" || Extension == "jpeg" || Extension == "png" || Extension == "gif" || Extension == "bmp") {
args.IsValid = true; // Valid file type
}
else {
args.IsValid = false; // Not valid file type
}
}
}
</script>