简体   繁体   English

自定义验证程序不适用于文件上传大小asp.net

[英]custom validator is not working for file upload size asp.net

I want to show error message when user tries to upload a file greater than 10 Mb in size. 当用户尝试上传大于10 Mb的文件时,我想显示错误消息。 Here is my code for validator: 这是我的验证器代码:

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="File size" ControlToValidate="attach" OnServerValidate="FileUploadCustomValidator_ServerValidate"></asp:CustomValidator>

Here is code for FileUploadCustomValidator_ServerValidate: 这是FileUploadCustomValidator_ServerValidate的代码:

protected void FileUploadCustomValidator_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        if (attach.HasFile)
        {
            if (attach.PostedFile.ContentLength > 10240)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
            }
        }
    }

For Attachment: 对于附件:

if (attach.HasFile)
            {
                attach.PostedFile.SaveAs(Server.MapPath("~/Data/") + attach.FileName);
                filename = attach.PostedFile.FileName.ToString();
                com.Parameters.AddWithValue("@attach", filename);
            }
            else
            {
                com.Parameters.AddWithValue("@attach", "");
            }

Now problem: it is not showing error message and not validating. 现在的问题:它没有显示错误消息并且没有验证。 Where is problem. 问题出在哪里。

Looks good, But I think you forgot to treat the condition that attach.HasFile is false, and also need to change the condition, as we know that 1 Byte = 0.000001 MB and you want to validate the file length by 10MB, make the following changes in the handler: 看起来不错,但我认为您忘记了对待attach.HasFile为false的条件,还需要更改条件,因为我们知道1 Byte = 0.000001 MB并且您想通过10MB验证文件长度,所以请执行以下操作处理程序中的更改:

 if (attach.HasFile)
 {
    if ((int)(attach.PostedFile.ContentLength * 0.000001) < 10)
    {
        e.IsValid = true;
    }
    else
    {
        e.IsValid = false;
    }
 }
 else
 {
     e.IsValid = false;
 }

I see two problems in your code. 我在您的代码中看到两个问题。 You want validation to fail if file size is greater than 10MB , but your code makes validation fail only if file size is less than 10KB . 您希望文件大小大于 10MB时验证失败,但是您的代码仅使文件大小小于 10KB才能使验证失败。 This is what you should do: 这是您应该做的:

if (attach.PostedFile.ContentLength > 10485760) // 10MB = 10 * (2^20)
{
    e.IsValid = false;
}
else
{
    e.IsValid = true;
}

Try the code 尝试代码

    private const int fileLengthPerMB = 1048576;
    private const int permitedFileSize = 10;
    int postedFileLength = fuHolterDiary.PostedFile.ContentLength;
    if ((postedFileLength / fileLengthPerMB) <= permitedFileSize)
    {
      e.IsValid = true;
    }
    else
    {
      e.IsValid = false;
      lblError.Text = "File size is too large. Maximum size permitted is 10 MB.";                           
    }

You can change the file size by changing the const 您可以通过更改const来更改文件大小

Remove ControlToValidate="attach" and try again. 删除ControlToValidate =“ attach”,然后重试。 You don't have to use this attribute as it will not work in many cases. 您不必使用此属性,因为在许多情况下它将不起作用。

What you should do is target the fileUpload control directly inside the validator event. 您应该做的是直接在验证事件中定位fileUpload控件。 Do something like this: 做这样的事情:

protected void FileUploadCustomValidator_ServerValidate(object sender, ServerValidateEventArgs args)
{
    if (fileUpload1.HasFile)
    {
        if (fileUpload1.PostedFile.ContentLength < 4024000) // if file is less than 3.8Mb
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

EDIT: Also change the last parameter in your event from "e" to "args" and do the same inside the code block. 编辑:还可以将事件中的最后一个参数从“ e”更改为“ args”,并在代码块内执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM