title: get posted file extension asp.net
body: <p>I keep getting "Only images are allowed" and I tried "file.PostedFile.FileName" also not working!!</p>
  
  <p>this code is written in a separate class..</p>
  
  <pre><code>public static String UploadFile(FileUpload file, String type, out String filename)
  {
      String ext = System.IO.Path.GetExtension(file.FileName);
      filename = "";
      if (file.PostedFile.ContentLength &gt; 2000000)
      {
          return "File is larger than 2 MB";
      }
      else if (type != "File")
      {
          if (ext.ToLower() != ".jpg" || ext.ToLower() != ".png" || ext.ToLower() != ".gif" || ext.ToLower() != ".jpeg")
          {
              return "Only images are allowed";
          }
          else
          {
              filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
              String root = HttpContext.Current.Server.MapPath("~/Images/");
              file.SaveAs(root + type + "/" + filename);
              return "Success";
          }
      }
      else
      {
          filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
          String root = HttpContext.Current.Server.MapPath("~/Files/");
          file.SaveAs(root + filename);
          return "Success";
      }
  }
  </code></pre>