简体   繁体   中英

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. 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:

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:

 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 . 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

Remove ControlToValidate="attach" and try again. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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