Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an Fileupload that i only want to use for images that i have created from the toolbox but when browing i only want to allow picuture extensions? anyone that know how i could start?
Posted

1 solution

You can get uploaded file's Extension like this and then validate or compare with your predefined list of extensions like this:

C#
if (fileupload1.HasFile)
{
FileInfo finfo = new FileInfo(fileupload1.FileName);
string fileExtension = finfo.Extension.ToLower();
/*compare this with a list of acceptable extensions..
 if it contains in list then
 fileupload1.SaveAs(filename);
 else return with a message..*/

}


Or by javascript you can try like this..
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
  <script language="JavaScript">
      function CheckType() {
          var id = document.getElementById('fileupload1').value;

          if (id != '') {
              var myextensions = /(.jpg|.jpeg|.gif)$/i;
              if (myextensions .test(id)) {
                  alert('OK');
              }
              else {
                  alert('Invalid file type.')
              }
          }
      }
</script>
</head>
<body>
    <asp:FileUpload ID="FileUpload1" runat="server" onChange="CheckType()" />
</body>
</html>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900