-3

if (FileUpload1.HasFile) { string extension = System.IO.Path.GetExtension(FileUpload1.FileName);

if (extension == ".jpg")
{
    FileUpload1.SaveAs("yourpath" + FileUpload1.FileName);

}
else
{
Response.Write("Only .Jpg allowed");
}

}

1 Answers1

-2

Try following :

            string[] imageTypes = {"jpg", "jpeg","bmp","gif", "png"};
            string filename = "abc.jpg";
            Boolean validFileName = imageTypes.Contains(filename.Substring(filename.LastIndexOf(".") + 1).ToLower());
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This will not work if the uploaded file's name ends in a dot, or has a non-lowercase extension. – CodeCaster Jul 08 '17 at 10:22
  • CodeCaster : you are referring to invalid file names. My code works with valid file extensions. – jdweng Jul 08 '17 at 11:03
  • 1
    What I mention (files ending in a dot and files with not entirely lowercase extensions) are **not** invalid filenames. This code is not robust. – CodeCaster Jul 08 '17 at 12:06
  • This question is not a duplicate of the ones reference. I added ToLower() to handle both upper and lower case. My answer should not be getting a negative rating. – jdweng Jul 09 '17 at 09:09