title: Upload file image validation
body: <p>I have these codes as my validation before someone can upload an image. however when i try to upload different files like video files and etc. it is still pushing through? what am i missing here? here is my whole code behind. im not sure what you are looking for im sorry. its just its accepting everything that i try to upload and it uploads it but no image.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (Session["IslandGasAdminPM"] != null)
          {
              if (!IsPostBack)
              {
                  GetCategories();
  
                  AddSubmitEvent();
              }
              if (Request.QueryString["alert"] == "success")
              {
                  Response.Write("&lt;script&gt;alert('Record saved successfully')&lt;/script&gt;");
              }
          }
          else
          {
              Response.Redirect("LogIn.aspx");
          }
      }
      private void AddSubmitEvent()
      {
          UpdatePanel updatePanel = Page.Master.FindControl("AdminUpdatePanel") as UpdatePanel;
          UpdatePanelControlTrigger trigger = new PostBackTrigger();
          trigger.ControlID = btnSubmit.UniqueID;
  
          updatePanel.Triggers.Add(trigger);
      }
      private void GetCategories()
      {
          ShoppingCart k = new ShoppingCart();
          DataTable dt = k.GetCategories();
          if (dt.Rows.Count &gt; 0)
          {
              ddlCategory.DataValueField = "CategoryID";
              ddlCategory.DataTextField = "CategoryName";
              ddlCategory.DataSource = dt;
              ddlCategory.DataBind();
          }
      }
  
      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
  
      }
  
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          if (uploadProductPhoto.PostedFile != null)
          {
              SaveProductPhoto();
  
              ShoppingCart k = new ShoppingCart()
              {
                  ProductName = txtProductName.Text,
                  ProductImage = "~/ProductImages/" + uploadProductPhoto.FileName,
                  ProductPrice = txtProductPrice.Text,
                  ProductDescription = txtProductDescription.Text,
                  CategoryID = Convert.ToInt32(ddlCategory.SelectedValue),
                  TotalProducts = Convert.ToInt32(txtProductQuantity.Text)
              };
              k.AddNewProduct();
              ClearText();
              Response.Redirect("/Admin/AddNewProduct.aspx?alert=success");
          }
          else
          {
              Response.Write("&lt;script&gt;alert('Please upload photo');&lt;/script&gt;");
          }
      }
      private void ClearText()
      {
          uploadProductPhoto = null;
          txtProductName.Text = String.Empty;
          txtProductPrice.Text = String.Empty;
          txtProductDescription.Text = String.Empty;
          txtProductQuantity.Text = String.Empty;
      }
      private void SaveProductPhoto()
      {
          if (uploadProductPhoto.PostedFile != null)
          {
              string filename = uploadProductPhoto.PostedFile.FileName.ToString();
              string fileExt = System.IO.Path.GetExtension(uploadProductPhoto.FileName);
  
              //check filename length
              if (filename.Length &gt; 96)
              {
                  Response.Write("Image should not exceed 96 characters");
              }
              //check file type
              else if (fileExt != ".jpg" &amp;&amp; fileExt != ".jpeg" &amp;&amp; fileExt != ".png" &amp;&amp; fileExt != ".bmp")
              {
                  Response.Write("Only jpg,jpeg,bmp and png are allowed");
              }
              //check file size
              else if (uploadProductPhoto.PostedFile.ContentLength &gt; 4000000)
              {
                  Response.Write("Image should not exceed 4MB");
              }
              //Save images to folder
              else
              {
                  uploadProductPhoto.SaveAs(Server.MapPath("~/ProductImages/" + filename));
              }
          }
  </code></pre>