简体   繁体   中英

File Upload Validation asp

I try to validate FileUpload control to check the extension of files by this regular expression

<asp:RegularExpressionValidator ID="RegularExpressionValidator5" ValidationGroup="PersonalGroup" 
           ControlToValidate="FileUpload1" Runat="Server"
            ErrorMessage="Only .pdf & .doc files are allowed" ValidationExpression="[^\.]\.pdf\.docx\.doc$" />*</asp:RegularExpressionValidator>

my problem to check the FileUpload already has file or not before i click the button submit because i got NullReferenceException if don't have file i try to make this

if (FileUpload1.HasFile)
                {
                    book.Book_File = System.IO.Path.GetPathRoot(FileUpload1.PostedFile.FileName);
                }

but the problem still exist if the user didn't uploaded any file. How to check the FileUpload is required filed before submit?

Use RequiredFieldValidator . Its available in the controls list.

Something like :

<asp:RequiredFieldValidator ID="rfvFileupload" runat="server" Display="Dynamic" ErrorMessage="Image is Required !" ControlToValidate="yourFileUploadID"></asp:RequiredFieldValidator>

You could also use below condition check behind your button:

If (page.isValid) {
//here goes your code behind button
}

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