Posts Tagged ‘check extension before upload’

To validate a file type or say file extension on client side before you upload a file can be performed by just putting a Regular Expression Validator. Here, i have provided one example to allow uploading only .jpg or .gif files. All you need is a FileUpload control and a Regular Expression Validator to check file type.

<asp:FileUpload ID=”FileUpload1″ runat=”server” />

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″ runat=”server” ControlToValidate=”FileUpload1″ Display=”Dynamic” ErrorMessage=”Upload a valid file” ValidationExpression=”^.+(.jpg|.JPG|.gif|.GIF)$”></asp:RegularExpressionValidator>

If you want to put your own file type validation then just replace “.jpg,.JPG,.gif,.GIF” text, and you can add more file types by adding “|” (pipe) sign and your own file type to validate.

Happy Coding!!