簡體   English   中英

ASP.NET文件大小驗證器無法驗證

[英]ASP.NET file size validator not validating

所以我的aspx文件中有一些ASP.NET文件上傳控件-

 <asp:FileUpload ID="fulNature" class="form-control summer" runat="server" /><asp:Labe ID="lblNature" runat="server" Text="Label" Visible="false"></asp:Label>
 <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="fulNature" Display="Dynamic"
                        ErrorMessage="File size should not be greater than 1 MB." ForeColor="#FF3300" OnServerValidate="ValidateFileSize"></asp:CustomValidator>

<asp:FileUpload ID="fulIndustrial" class="form-control summer" runat="server" /><asp:Label ID="lblIndustrial" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="fulIndustrial" Display="Dynamic"
                        ErrorMessage="File size should not be greater than 1 MB." ForeColor="#FF3300" OnServerValidate="ValidateFileSize"></asp:CustomValidator>

我正在嘗試使用一個C#驗證器來限制文件大小-

protected void ValidateFileSize(object source, ServerValidateEventArgs args)
{
    var validator = (source as CustomValidator);
    string controlToValidate = validator.ControlToValidate;
    FileUpload SubmittedUpload = validator.NamingContainer.FindControl(controlToValidate) as FileUpload;

    if (SubmittedUpload.FileBytes.Length > 1048576) //  A little padding added.
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}

問題在於,無論文件大小如何,驗證器都不會觸發。 我必須在這里缺少一些簡單的東西,但是我只是沒有看到它。

您可以直接獲取上傳文件的大小:

long size=fulNature.FileContent.Length;

嘗試:

long filesize = SubmittedUpload.FileContent.Length;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM