Friday, April 2, 2010

How to filter file extension and get file size in C# ?

 Here form this function I'm trying to filter extension and size for uploading  a file in asp.net
public static string FilteredFileExtension(HttpPostedFile httpPostedFile)
    {
        int intFileLength = httpPostedFile.ContentLength;   // File size
        string strExtn = System.IO.Path.GetExtension(httpPostedFile.FileName.ToLower()); //File extension
        if (intFileLength < 4096000)// 4mb
        {
            if (strExtn == ".jpg" || strExtn == ".gif" || strExtn == ".png")
            {
                return string.Empty;  // only jpg,gif and png files and less dn 4mb can be uploaded
            }
            else
            {
                return strExtn + " File not accepted";
            }
        }
        else
        {
            return "File is very large";
        }
    }