Using Regular Expressions to validate a filename in a FileUpload control
Here's a little code snippet I use to ensure that the file that has been uploaded is of type JPG, JPEG, PNG or GIF
// check anything has been uploaded
if (ThumbnailImageUpload.PostedFile.ContentLength > 0)
{
// the regex for an image
Regex imageFilenameRegex = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$");
if (imageFilenameRegex.IsMatch(ThumbnailImageUpload.PostedFile.FileName,
RegexOptions.IgnoreCase))
{
// we have a valid filename
// .. do something ..
}
}
| Author |
: Paul Hayman |
| Published |
: 31 July 2006 |
Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.
Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.
Comments