Validating Extension Before Uploading File in ASP.Net

Sometimes the client needs to upload files to the server or into a database like photos or Word documents or PDF and so on. So for that you need to check the extension of the file.

The following are some of the basic files with their extension.



To prevent this kind of problem, you need to validate the file on the client side as well as the server side. To learn more about that see the following.

Add a new "Website" named "Website1".



And you will get the default page named "Default.aspx".



Add a File upload control named "FileUpload1" and button with "Upload" text on the page.



Client Side: This is how to add the validation on the client side. Add the JavaScript code into the <script> tag as in the following:

  1. function GetExt()   
  2.      {  
  3.             var str = document.getElementById('FileUpload1').value;  
  4.             var ext = str.substring(str.length - 3, str.length).toString();  
  5.             extext = ext.toLowerCase();  
  6.             if (ext == "pdf")
  7.             {      
  8.                alert("valid File")             
  9.                 return true;  
  10.             }  
  11.             else
  12.             {  
  13.                 alert("Invalid File"); return false;  
  14.             }  
  15.         }  
Call the function on the client click event of button.
  1. OnClientClick="return GetExt();"  


Result
  • If I select the PDF file then:


And click on the upload button, it will show to me that it's a valid file.


  • If I select the text file 

 And click on the upload button, it will show to me that it's an invalid file.



Server Side: This is how to add validation on the server side.

Note: I will explain it after removing the preceding JavaScript code from the page.

Add the server code on the click event of the button.
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.     {  
  3.         string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);  
  4.         string extension = Path.GetExtension(filename);  
  5.         string contentType = FileUpload1.PostedFile.ContentType;  
  6.         HttpPostedFile file = FileUpload1.PostedFile;  
  7.         byte[] document = new byte[file.ContentLength];  
  8.         file.InputStream.Read(document, 0, file.ContentLength);  
  9.   
  10.         if (extension != ".pdf")  
  11.         {  
  12.             //  file is Invalid  
  13.             Response.Write("This is Invalid Extension File");  
  14.             return;  
  15.         }         
  16.         else  
  17.         {             
  18.             // upload the File because file is valid  
  19.             Response.Write("This is valid File");  
  20.         }  
  21.     }  

 
Now if I run the page it will look like:



Case 1: In this case I will upload a .png file which is an invalid file.



And the output will be "Invalid Extension file".



Case 2: In this case I will upload a valid PDF file.



And the output will be "Valid file".

Up Next
    Ebook Download
    View all
    Learn
    View all