【问题标题】:Number of Uploaded files function does not show the error messageNumber of Uploaded files 功能不显示错误信息
【发布时间】:2020-09-14 00:28:06
【问题描述】:

我正在尝试将可以上传的文件数量限制为 2。我有以下验证器和代码来解决此问题:

  <asp:ValidationSummary runat="server" ID="ValidationSummary1" 
                   DisplayMode="BulletList" ShowMessageBox="false" ShowSummary="True" CssClass="alert alert-danger" />

     <asp:FileUpload runat="server" ID="UploadPDF" AllowMultiple="true" accept=".pdf"  /> 
    
     <asp:CustomValidator Display="None" ID="customValidatorUpload" runat="server" ErrorMessage="Only five files can be uploaded" ControlToValidate="UploadPDF" ClientValidationFunction="ValidateFile2();" />

下面是 ValidateFile2() 函数:

   <script>
        function ValidateFile2(sender, args) {
            
            var fileCount = document.getElementById('UploadPDF').files.length;
            if (fileCount > 2) 
            {
                args.IsValid = false;
               
            }
            else if (fileCount <= 0) 
            {
                args.IsValid = false;
            }

            args.IsValid = true; 
        }
    </script>

当我尝试上传超过 2 个文件时,我没有看到任何错误消息。我不确定我做错了什么。

我试着放了

console.log(fileCount);

在 ValidateFile2 函数中,这是我在浏览器控制台下看到的错误:

这是我在检查元素时看到的:

uploadcerts 是我的页面名称。

任何帮助将不胜感激。

【问题讨论】:

  • args 未定义,如错误所示。
  • 这里 ClientValidationFunction="ValidateFile2();" 你也需要传递参数,即:senderargs
  • ClientValidationFunction="ValidateFile2();"改成ClientValidationFunction="ValidateFile2"怎么样?这有什么区别吗?
  • 另外,由于UploadPDF是.net控件,你需要使用var fileCount = document.getElementById('&lt;%=UploadPDF.ClientID%&gt;').files.length;
  • @SelimYildiz 检查图片 - ID 没问题。

标签: c# jquery asp.net


【解决方案1】:

这是我为解决问题所做的。我从 ValidateFile2 函数中删除了括号,代码立即开始工作。以下是更新后的代码:

  <asp:ValidationSummary runat="server" ID="ValidationSummary1" 
                       DisplayMode="BulletList" ShowMessageBox="false" ShowSummary="True" CssClass="alert alert-danger" />
    
         <asp:FileUpload runat="server" ID="UploadPDF" AllowMultiple="true" accept=".pdf"  /> 
        
         <asp:CustomValidator Display="None" ID="customValidatorUpload" runat="server" ErrorMessage="Only five files can be uploaded" ControlToValidate="UploadPDF" ClientValidationFunction="ValidateFile2" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多