Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Check File Extension In Array Of Valid Extension

C# Check File Extension In Array Of Valid Extension

Assume that we’re building an upload function that allow people be able to upload either photo or video to server. Before storing the uploaded file to hard drive of web server, we need to verify that file extension is valid. And file extension validation is the first and important step in the checking process.

This post provides a simple function (with 2 input parameters: extension and type string) that validate file extension for video and photo. And each part will have an array of accepted extensions.

C# Check File Extension

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class check_valid_extension : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int is_valid = 0;
 
        is_valid = chkValidExtension(".jpg", "photo");
 
        if (is_valid == 1)
        {
            Response.Write("The file extension is valid");
        }
        else if (is_valid == 0)
        {
            Response.Write("The file extension is not valid");
        }
    }
 
    public int chkValidExtension(string ext, string type)
    {
        if (type == "photo")
        {
            string[] PosterAllowedExtensions = new string[] { ".gif", ".jpeg", ".jpg", ".png" };
            for (int i = 0; i < PosterAllowedExtensions.Length; i++)
            {
                if (ext == PosterAllowedExtensions[i])
                    return 1;
            }
 
            return 0;
        }
        else if (type == "video")
        {
            string[] TrailerAllowedExtensions = new string[] { ".flv", ".asf", ".avi", ".wmv", ".mp4", ".mov", ".3gp", ".m4v", ".3g2" };
            for (int i = 0; i < TrailerAllowedExtensions.Length; i++)
            {
                if (ext == TrailerAllowedExtensions[i])
                    return 1;
            }
 
            return 0;
        }
 
        return 0;
    }
}

using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class check_valid_extension : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int is_valid = 0; is_valid = chkValidExtension(".jpg", "photo"); if (is_valid == 1) { Response.Write("The file extension is valid"); } else if (is_valid == 0) { Response.Write("The file extension is not valid"); } } public int chkValidExtension(string ext, string type) { if (type == "photo") { string[] PosterAllowedExtensions = new string[] { ".gif", ".jpeg", ".jpg", ".png" }; for (int i = 0; i < PosterAllowedExtensions.Length; i++) { if (ext == PosterAllowedExtensions[i]) return 1; } return 0; } else if (type == "video") { string[] TrailerAllowedExtensions = new string[] { ".flv", ".asf", ".avi", ".wmv", ".mp4", ".mov", ".3gp", ".m4v", ".3g2" }; for (int i = 0; i < TrailerAllowedExtensions.Length; i++) { if (ext == TrailerAllowedExtensions[i]) return 1; } return 0; } return 0; } }

First you need to get file extension from the uploaded file and pass it to chkValidExtension function as “ext” parameter and pass “video” or “photo” for “type” parameter depend on your need.

Dec 4, 2011Hoan Huynh
C# Get File Extension Without Sub String Or Split FunctionMost Popular Video And Photo File Extension
You Might Also Like:
  • Most Popular Video And Photo File Extension
  • C# Get File Extension Without Sub String Or Split Function
  • Get File Mime Type Using PHP
  • C# Read File Content
  • PHP Convert stdClass Object To Array And Array To stdClass Object
  • Get Or Read AppSettings Item Value From The Web.Config File
  • C# Check Google PageRank – ASP.NET Example
  • ASP.Net C# Download Or Save Image File From URL
  • PHP Read File Using Fread() Simple Example
  • CentOS install PHP Json Extension by using PECL
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpArray, Extension1,374
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,558 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,892 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,746 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,071 views
C# Read Json From URL And Parse/Deserialize Json
11,805 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development