Filter the upload filetype – ASP.NET

If you want to filter the filetype in a fileupload control:

< asp:FileUpload ID="AttachmentFile" runat="server" Width="240px"
style="background-color: #FFF0E1" />

< asp:RegularExpressionValidator id="FileUpLoadValidator" runat="server"
ErrorMessage="Th file type is not allowed."
ValidationExpression="^.+(.doc|.DOC|.pdf|.PDF)$"
ControlToValidate="AttachmentFile" style="font-size: x-small" >

This will only allow to upload doc and pdf file types.

You could use other methods to validate/filter the file types.
The following code should put under aspx.vb file.

Dim fileName As String = yourattachmentfield.PostedFile.FileName
Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))
If ext = ".doc" Then
ElseIf ext = ".RTF" Then
Else
Label1.Text = "doc and pdf file formats only supported."
Label1.ForeColor = Drawing.Color.Red
Exit Sub
End If