ASP.Net FileUpload File type filter Validation using C# code

Updated on 04 Jul 2012,
Published on 30 Oct 2009

In the previous tutorial: ASP.Net FileUpload File type filter using JavaScript validation we learnt about importance of this validation to disallow the user from uploading the file with in-appropriate file type extensions. As we discussed there may be different types of requirements for different types of features in a web application where the FileUpload control can be used to provide the functionality of file uploading from client’s machine to the online web server. You may need to validate the FileUpload control by applying the file type filter to disable the user to upload the files having file types extensions of image files or you may need the validation function to validate the control to disable the users from uploading the files that are not images. But the ASP.Net FileUpload control does not provide any in-built method to customize the file type filter dropdown of file-navigation dialog box to a specific file type. Like we applied JavaScript Code in the previous tutorial here we will create the similar server side code using C#.

It is not recommended to rely totally on JavaScript validation as it can be disabled by the user by using web browser preferences. That is why C# code is also required at the server end to revalidate the input file type extension before uploading it.

Sample Code to Implement the File Type Filter on FileUpload Control

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" 
    OnClick="btnUpload_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
C# Code
protected void btnUpload_Click(object sender, EventArgs e)
    // store the file name into the string variable
    string fileName = FileUpload1.PostedFile.FileName;

    // extarct and store the file extension into another variable
    string fileExtension = System.IO.Path.GetExtension(fileName).Replace(".", string.Empty).ToLower();

    // string type array having list of allowed file type extensions
    string[] validFileExtensions = new string[] { "gif", "png", "jpg" };

    bool flag = false;

    // loop over the array of valid file extensions to compare them with uploaded file
    foreach (string extension in validFileExtensions)
        if (fileExtension == extension)
            flag = true;

    // display the message based on the flag value
    if (flag == false)
        Label1.Text = "Files with extension <b>\"" + fileExtension + "\"</b> are not allowed.<br />";
        Label1.Text += "You can upload the files with following extensions only: <br />";
        Label1.Text += ".gif<br />.png<br />.jpg";
        Label1.Text = "File has valid extension.";

        // write your code to save the uploaded file here.

In the above C# code we have used a string type array to store the list of allowed file type extensions. Next we have used the "C# foreach loop" over the array of allowed file extensions to compare each file extension with the extension of the file uploaded by the user. A Boolean type flag is used in the above code to store the true/false type value to set a flag value that will indicate whether the file extension matched with the allowed file types or not. The "true" value for flag variable will indicate that the uploaded file has a valid file extension or not and you can execute the code according to it.

Also read the tutorial: ASP.Net FileUpload control to learn about the different types of properties available to upload and save the file on the web server hard disk.

Continue to next tutorial: HiddenField Control in ASP.Net to learn about the properties of HiddenField control.

0 Responses to "ASP.Net FileUpload File type filter Validation using C# code"
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email