Friday, 19 October 2012

Restricting File Type for Dropdownlist and Displaying Error for the same.


Restricting File Type for Dropdownlist and Displaying Error for the same


Here is another method to upload the specific file type using fileupload control..


    protected void Button1_Click(object sender, EventArgs e)

    {

        int flag = 1;

        try

        {

            DataSet dsSelectGalleryHead = objGalleryPic.SelectAllGalleryPic();

            for (int i = 0; i < dsSelectGalleryHead.Tables[0].Rows.Count;i++ )

            {

                string str = dsSelectGalleryHead.Tables[0].Rows[0].ItemArray[1].ToString();

                if (str == txtGalleryHead.Text)

                {

                    flag = 0;

                }

            }

            if (flag == 0)

            {

                objGalleryPic.GalleryHead = txtGalleryHead.Text;

                if (fupGalleryImage.HasFile)

                {

                    if (CheckFileType(fupGalleryImage.FileName))

                    {

                        string smlFilePath = "~/GalleryPic/" + fupGalleryImage.FileName;

                        fupGalleryImage.SaveAs(MapPath(smlFilePath));

                        objGalleryPic.GalleryPicImage = smlFilePath;

                    }

                    //else

                    //{

                    //    WebMsgBox.Show("Please Enter a Valid Format.");

                    //}

                }

                if (objGalleryPic.InsertGalleryPic() > 0)

                {

                    lblMsg.Text = "Gallery Picture successfully updated.";

                    grdGalleryPic.Visible = true;

                    bindGrdGalleryPic();

                }

            }

            else if (flag == 1)

            {

                objGalleryPic.GalleryHead = txtGalleryHead.Text;

                objGalleryPic.ThumbnailHead = txtGalleryHead.Text;

                if (fupGalleryImage.HasFile)

                {

                    if (CheckFileType(fupGalleryImage.FileName))

                    {

                        string smlFilePath = "~/GalleryPic/" + fupGalleryImage.FileName;

                        fupGalleryImage.SaveAs(MapPath(smlFilePath));

                        objGalleryPic.GalleryPicImage = smlFilePath;

                        objGalleryPic.ThumbnailImage = smlFilePath;

                    }

                    //else

                    //{

                    //    WebMsgBox.Show("Please Enter a Valid Format.");

                    //}

                }

                if (objGalleryPic.InsertGalleryPic() > 0)

                {                   

                    lblMsg.Text = "Gallery Picture successfully updated.";

                    grdGalleryPic.Visible = true;

                    bindGrdGalleryPic();

                }

                DataSet dsSelectGalleryPic = objGalleryPic.SelectAllGalleryPic();

                objGalleryPic.GalleryPicId = Convert.ToInt32(dsSelectGalleryPic.Tables[0].Rows[0].ItemArray[0]);

                objGalleryPic.InsertThumbnail();               

            }           

        }

        catch (Exception ex)

        {

            if (CheckFileType(fupGalleryImage.FileName)==false)

            {

                WebMsgBox.Show("Format Type should be: JPEG/JPG/GIF/PNG/BMP or TIF");

                //lblMsg.Text = "Please Enter a Valid Format.";

            }           

           else

            {

                WebMsgBox.Show(ex.Message);

            }

        }

    }

    public bool CheckFileType(string fileName)

    {

        string ext = Path.GetExtension(fileName);

        switch (ext.ToLower())

        {

            case ".gif":

                return true;

            case ".jpg":

                return true;

            case ".jpeg":

                return true;

            case ".png":

                return true;

            //case ".doc":

            //    return true;

            //case ".docs":

            //    return true;

            //case ".pdf":

            //    return true;

            case ".bmp":

                return true;

            case ".tif":

                return true;

            default:

                return false;

        }

    }

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top