Friday, 08 March 2013 13:54

how to know the file extension of file in asp.net

Written by 
Rate this item
(0 votes)

in some way these codes will help you very much if you need
to know file extension.
method below returns true if extension are any of the following -

protected Boolean CheckFileType(String fileName)
{
String ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".gif": return true;

case ".png": return true;

case ".jpg": return true;

case ".jpeg": return true;

case ".bmp": return true;

default: return false;
}
}

i am calling this method by this way from another method -

if (fileUpload.HasFile) //here fileUpload is a asp.net FileUpload control
{
if ((CheckFileType(fileUpload.FileName))) { }
else { ErrorMessage.Text = "Please select a picture with a file
extension of either: .bmp .jpg .gif .png"; return; }
}

Read 2790 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.