How to check and validate file extension before uploading a file through FileUpload control in | Use of RegularExpressionValidator control to validate file extension before uploading file through FileUpload control in

Introduction: In previous articles i explained Drag & drop to upload multiple files using AjaxFileUpload like Facebook in and How to call java script function from code behind file in and What is Page.IsValid and Page.Validate in and How to read from XML file and bind to gridview in and How to create thumbnail, small and large version of the uploaded image in and How to send emails in | How to set Smtp setting in web.config file to send email in
In this article i will explain how to restrict user to upload the files of specific type through FileUpload control by validating through RegularExpressionValidator validation control.

Description: Sometimes it is required to validate the file extension before uploading file i.e. you want that user can upload only the files that you want e.g. if you want the user can upload only Word  and Excel  files(.doc, .docx, .xls, .xlsx ) then you have to check the extension of the file the user is going to upload. Similarly if you want only images of the type (.png, .jpg, .jpeg, .bmp or.gif) can be uploaded then it is also possible.

Implementation: Let's create an website to understand.
  • Place a FileUpload control and the RegularExpressionValidator control , ValidationSummary control and a Button Control on design page(.aspx)
  <asp:FileUpload ID="FileUpload1" runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ErrorMessage="Only excel or word file is allowed!"
ValidationExpression ="^.+(.xls|.XLS|.xlsx|.XLSX|.doc|.DOC|.docx|.DOCX)$"

<asp:ValidationSummary ID="ValidationSummary1" runat="server"
            ShowMessageBox="True" ShowSummary="False" />

<asp:Button ID="btnUpload" runat="server"  Text="upload"
            onclick="btnUpload_Click" />

C#.Net Code to check and validate file extension before uploading a file
  • In the code behind (.aspx.cs) file write the code:
    protected void btnUpload_Click(object sender, EventArgs e)
            //write code to upload file
            string filePath = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.PostedFile.FileName);

VB.Net Code to check and validate file extension before uploading a file
  • In the code behind (.aspx.vb) file write the code:
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)     
                                         'write code to upload file
                                         Dim filePath As String = (Server.MapPath("Uploads/") + Guid.NewGuid() + FileUpload1.PostedFile.FileName)
End Sub

Note:  If you want to upload only image files e.g. .png,.jpg,.jpeg,.bmp or gif then write ValidationExpression ="^.+(.png|.jpg|.jpeg|.bmp|.gif|)$".

In this example Uploaded file will be saved in the Uploads folder. You can create a folder with different name and Replace the Uploads folder with the name of your Folder.

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."

Next Post »


Click here for comments
April 10, 2014 ×

Nice Article.......

April 10, 2014 ×

Hi Alekhya Kusuma..thanks for your feedback..i am glad you liked my article..stay connected and keep reading...:)


If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..