Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

through fileupload control choose specific extension file

0.00/5 (No votes)
27 Nov 2011 1  
if(IsPostBack){Boolean fileOK=false;String fileExtention=System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();   //check file

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

if(IsPostBack)

{

Boolean fileOK=false;

String fileExtention=System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();   //check file extension

Response.Write(Extension is..."+fileExtension);

String[] allowedExtension={".gif",".png"}; //only two file extension are accepted

for(int i=0;i<allowedExtension.Length;i++)

{

if(fileExtention==allowedExtension[i])   //check both extension are same

{

fileOK=true;

}}}

if(fileOK)

{try{FileUpload.PostedFile.SaveAs(path+FileUpload1.FileName);

Label1.Text="FileUploaded!";

}

catch(Exception ex)

{

Label1.Text="File could not be uploaded";

}}

else

{

Label1.Text="cannot accept files of this type";

}

}

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here