0

I am using file upload in visual studio 2013in asp.net c#.I have make validation for only image file must be upload but when i renamed the file extension try.jpg to try.jpg.txt . it will be upload. I want that only valid type file should be upload. so how i prevent it.

here is my code.

string ImageExt = Path.GetExtension(fuBlogImageFile.FileName).ToLower();
                if (ImageExt == ".jpg" || ImageExt == ".jpeg" || ImageExt == ".bmp" || ImageExt == ".png" || ImageExt == ".gif")
                {
                    string imgBlog = txtBlogTitle.Text.Trim().Replace(" ", "").Replace("?", "").Replace(".", "").Replace(",", "");
                    ImageName = CommonClass.commonFuntion.Shorten(imgBlog, 20, true) + ((System.DateTime.Now).ToString()).Replace(":", "").Replace("/", "").Replace(" ", "") + ImageExt;
                    ViewState["BlogImageFile"] = ImageName;
                    string targateFilePath = Server.MapPath("~") + CommonClass.Admin.ImagePath.BlogImageFile + ImageName;
                    csImageResize objImageResize = new csImageResize();
                    ResutImageSave = objImageResize.ImageWidthHeightDecrisce(fuBlogImageFile.PostedFile.InputStream, 728, 241, 728, 241, targateFilePath, false);
                    if (ResutImageSave.Equals("Image Save Successfully"))
                    {
                        returnstring = ResutImageSave;
                        //divError.Visible = false;
                    }
                    else
                    {
                        returnstring = ResutImageSave;

                    }
                }
Udhay Titus
  • 5,761
  • 4
  • 23
  • 41
Naveen
  • 1,441
  • 2
  • 16
  • 41

2 Answers2

1

Please make sure that it is saving file if you renamed the file name.. i have tested for try.jpg.txt but it is showing me .txt extension of the file. you can see here

mmushtaq
  • 3,430
  • 7
  • 30
  • 47
  • 1
    Now i want just check that file is actual valid file or not, saving file is different matter. – Naveen Jul 15 '16 at 13:26
0

Few possible options can be: 1)

if (fuBlogImageFile.ContentType == "image/jpeg")
{
      //its a jpeg
}

2) Custom c# class to determine file type

Hope this helps!

Community
  • 1
  • 1
Sami
  • 3,686
  • 4
  • 17
  • 28