ASP.NET文件上载验证

ASP.NET文件上载验证,asp.net,validation,file-upload,Asp.net,Validation,File Upload,我需要对文件上载控件进行多次验证。我现在有以下代码: <asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server" OnClick="UploadFile_Click" Text="Upload File"></asp:Button> <asp:RequiredFieldValidator ID="InputFileValidator" ControlToValidate="

我需要对文件上载控件进行多次验证。我现在有以下代码:

<asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server"   OnClick="UploadFile_Click" Text="Upload File"></asp:Button>
<asp:RequiredFieldValidator ID="InputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" runat="server" />

除了所需的字段验证器之外。我该怎么做?

更新:

您可能会改为使用正则表达式,以允许反斜杠到文件名,并在文件名中不允许反斜杠,但这样一个庞然大物的复杂性可能不值得花费时间和精力来构建它


由于原始正则表达式用于验证用户输入文件名的文本框(而不是由操作系统生成名称的文件输入),因此我认为更好的做法是使用

我尝试了这一点,但只要我浏览并添加一个文件,正则表达式验证就会失败。这可能是由于正则表达式中的“:”造成的。如何进行验证,使其只检查文件名而不检查路径?工作非常完美!我会记住Unix/mac文件路径名。非常感谢。
    <script type="text/javascript">
        var validateFile = function validateFile(sender, args) {
            'use strict';
            var fileWithPath, //split on backslash
                fileName = fileWithPath[fileWithPath.length - 1], //grab the last element
                containsInvalidChars = /["#%&*:<>?\/{|}~]/g, //no reason to include \ as we split on that.
                containsSequentialDots = /[.][.]+/g, //literal .. or ... or .... (etc.)
                endsWithDot = /[.]$/g, // . at end of string
                startsWithDot = /^[.]/g, // . at start of string
                notValid = false, //boolean for flagging not valid
                valid = fileName.length > 0 && fileName.length <= 128;
            notValid = containsInvalidChars.test(fileName);
            notValid = notValid || containsSequentialDots.test(fileName);
            notValid = notValid || endsWithDot.test(fileName);
            notValid = notValid || startsWithDot.test(fileName);
            args.IsValid = valid && !notValid;
        };
    </script>
    <asp:FileUpload ID="InputFile" runat="server" />
    <asp:RequiredFieldValidator ID="rqfvInputFile" runat="server" ControlToValidate="InputFile" ErrorMessage="File is required"></asp:RequiredFieldValidator>
    <asp:CustomValidator ID="cstvInputFile" runat="server" ControlToValidate="InputFile" ClientValidationFunction="validateFile" ErrorMessage="File is not a sharepoint file"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />
var filePath = args.Value.split(/\\|\//g); //not tested.
<asp:Button id="btnUploadFile" class="ms-ButtonHeightWidth" runat="server" OnClick="UploadFile_Click" Text="Upload File"></asp:Button>
<asp:RequiredFieldValidator runat="server" ID="RequiredInputFileValidator" ControlToValidate="InputFile" Text="You must specify a value for the required field" />
<asp:RegularExpressionValidator runat="server" ID="RegexInputFileValidator" ControlToValidate="InputFile" ErrorMessage="Only valid SharePoint files are allowed."
    ValidationExpression="^(?!..)(?!...)(?=.[^.]$)[^\"#%&:<>?\/{|}~]{1,128}$" />