Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i have problems with validating the file upload types. The rest are working fine but i have no idea how to validate the file types.

Below is the part where i validate my file types:

C#
private void StartUpLoad()
   {
       string companyID = this.Label1.Text;

       string imgName = FileUpload1.FileName;

       string imgPath = "~/Uploads/" + imgName;

       int imgSize = FileUpload1.PostedFile.ContentLength;

       string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);

       if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
       {

           if (FileUpload1.PostedFile.ContentLength > 1000000)
           {
               Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
           }
           if (ext != ".jpg" || ext != ".png" || ext != ".gif" || ext !=".jpeg")
           {
               Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
           }

           else
           {

               FileUpload1.SaveAs(Server.MapPath(imgPath));
               Image1.ImageUrl = imgPath;
               Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

               byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
               HttpPostedFile Image = FileUpload1.PostedFile;
               Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);

               int length = theImage.Length;
               string fileName = FileUpload1.FileName.ToString();
               string type = FileUpload1.PostedFile.ContentType;

               int size = FileUpload1.PostedFile.ContentLength;
               if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
               {

                   WP_advBLL canwork = new WP_advBLL();
                   canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
                   Response.Write("File has been uploaded successfully!");
                   Session["imageID"] = imgName;
               }
           }

       }

the bold lines are the validation for file types, but it is not working. any idea why?
Posted
Updated 10-Jul-21 3:33am
v2

C#
private void StartUpLoad()
    {
        string companyID = this.Label1.Text;
 
        string imgName = FileUpload1.FileName;
 
        string imgPath = "~/Uploads/" + imgName;
 
        int imgSize = FileUpload1.PostedFile.ContentLength;
 
        string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower();
 
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
 
            if (FileUpload1.PostedFile.ContentLength > 1000000)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
            }
            if (ext != ".jpg" || ext != ".png" || ext != ".gif" || ext !=".jpeg")
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
            }
 
            else
            {
 
                FileUpload1.SaveAs(Server.MapPath(imgPath));
                Image1.ImageUrl = imgPath;
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
 
                byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile Image = FileUpload1.PostedFile;
                Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);
 
                int length = theImage.Length;
                string fileName = FileUpload1.FileName.ToString();
                string type = FileUpload1.PostedFile.ContentType;
 
                int size = FileUpload1.PostedFile.ContentLength;
                if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
                {
 
                    WP_advBLL canwork = new WP_advBLL();
                    canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
                    Response.Write("File has been uploaded successfully!");
                    Session["imageID"] = imgName;
                }
            }
 
        }
   
i modified your code please see this

C#
string companyID = this.Label1.Text;

        string imgName = FileUpload1.FileName;

        string imgPath = "~/Uploads/" + imgName;

        int imgSize = FileUpload1.PostedFile.ContentLength;

        string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);

        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {

            if (FileUpload1.PostedFile.ContentLength > 1000000)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
            }
//modified this code 
            if (ext.ToUpper().Trim() != ".JPG"  && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".GIF" && ext.ToUpper() != ".JPEG")
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
            }

            else
            {

                FileUpload1.SaveAs(Server.MapPath(imgPath));
                Image1.ImageUrl = imgPath;
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

                byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile Image = FileUpload1.PostedFile;
                Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);

                int length = theImage.Length;
                string fileName = FileUpload1.FileName.ToString();
                string type = FileUpload1.PostedFile.ContentType;

                int size = FileUpload1.PostedFile.ContentLength;
                if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
                {

                      WP_advBLL canwork = new WP_advBLL();
                    canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
                    Response.Write("File has been uploaded successfully!");
                    Session["imageID"] = imgName;
                }
            }

        }
   
v3
Comments
RaviRanjanKr 9-Jul-13 5:59am
   
Always wrap your code in pre tags.
maneavnash 9-Jul-13 6:06am
   
thank
Jerrell77 10-Jul-13 9:23am
   
Thank you guys! This is the exactly what i need! :)
mossyelropy 16-May-14 9:01am
   
gooooooooooooooooooooooood job very nice !
Try this Regular Expression Validator:

XML
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                   ControlToValidate="fileupload1"
                   ErrorMessage="Only .jpg,.png,.jpeg,.gif Files are allowed" Font-Bold="True"
                   Font-Size="Medium"
                   ValidationExpression="(.*?)\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$"></asp:RegularExpressionValidator>
   
[this logic is wrong in this condition always true if i select .png image this is not .jpg then it will give error]
it should be !!!
if (ext == ".jpg" || ext == ".png" || ext == ".gif" || ext ==".jpeg")
{
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);

int length = theImage.Length;
string fileName = FileUpload1.FileName.ToString();
string type = FileUpload1.PostedFile.ContentType;

int size = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile != null &amp;&amp; FileUpload1.PostedFile.FileName != "")
{

WP_advBLL canwork = new WP_advBLL();
canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
Response.Write("File has been uploaded successfully!");
Session["imageID"] = imgName;
}
}

else
{

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);

}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900