需要帮助来纠正我使用fileupload检查文件扩展名的代码

需要帮助来纠正我使用fileupload检查文件扩展名的代码

本文介绍了需要帮助来纠正我使用fileupload检查文件扩展名的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


下面的按钮代码我遇到了问题,因为它想检查所有文件是否有文件然后检查所有上传文件扩展名是.jpg或.png如果是,则完成其余的代码

Hithe below code of button i am facing a problem with it, as it suppose to check if all files has file and then check all uploadfile extension with .jpg or .png if yes then complete the rest of the code

var files = new[] { LogoFileExtention, BizImg1FileExtention, BizImg2FileExtention, BizImg3FileExtention, PersImgFileExtention };
                        var extensions = new[] { ".jpg", ".png"};
                        if ((files.Intersect(extensions).Count()) > 0)
                        {





如果没有,请转到下面的代码,但正在发生的事情是,如果扩展名为.doc或.pdf,它通常不会停止在代码的下面部分完成



and if not then move to below code, but what is happening is if the extension is .doc or .pdf it complete as normally without stop on the below part of the code

else
                        {
                            imagesformtLbl.Text = "Images should be in .JPG or .PNG format only";
                        }







protected void btnSave_Click(object sender, EventArgs e)
        {
            string LogoFileExtention = System.IO.Path.GetExtension(logoFileUpload.FileName);
            string BizImg1FileExtention = System.IO.Path.GetExtension(FileUploadimage1.FileName);
            string BizImg2FileExtention = System.IO.Path.GetExtension(FileUploadImage2.FileName);
            string BizImg3FileExtention = System.IO.Path.GetExtension(FileUploadImage3.FileName);
            string PersImgFileExtention = System.IO.Path.GetExtension(persimgFileUpload1.FileName);


            if (citiesdrdolst.Items.Count == 0)
            {
                alertpanel.Visible = true;
                StateReqLbl.Text = "Please select state where you live.";
            }



            else
            {

                HttpCookie cookie = Request.Cookies.Get("Location");
                string Location = string.Empty;
                SqlConnection cn = new SqlConnection(sc);
                SqlCommand cmd = new SqlCommand();
                Location = cookie.Value;

                if (CheckBox1.Checked)
                {
                    if (logoFileUpload.HasFile || FileUploadimage1.HasFile || FileUploadImage2.HasFile || FileUploadImage3.HasFile || persimgFileUpload1.HasFile)
                    {


                        var files = new[] { LogoFileExtention, BizImg1FileExtention, BizImg2FileExtention, BizImg3FileExtention, PersImgFileExtention };
                        var extensions = new[] { ".jpg", ".png" };
                        if ((files.Intersect(extensions).Count()) > 0)
                        {

                                string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img, Logo,
RegDate,Address, UsrType,BizCateg,BizSubCateg, CompNme, Facebook, GooglePlus, Twitter, Website, image1, image2, image3) VALUES
(@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@Logo,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg,
@CompNme,@Facebook,@GooglePlus,@Twitter,@Website,@image1,@image2,@image3)";

                                cmd.Connection = cn;
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = sqlstatment;

                                //Insert the parameters first
                                cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
                                cmd.Parameters.AddWithValue("@FN", fnbox.Text);
                                cmd.Parameters.AddWithValue("@LN", lnamebox.Text);


                                }

                                Response.Redirect("User panel.aspx");
                            }

                        else
                        {
                            imagesformtLbl.Text = "Images should be in .JPG or .PNG format only";
                        }



                    }

                    else
                    {
                         string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img,
RegDate,Address, UsrType,BizCateg,BizSubCateg, CompNme, Facebook, GooglePlus, Twitter, Website) VALUES
(@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg,
@CompNme,@Facebook,@GooglePlus,@Twitter,@Website)";

                                cmd.Connection = cn;
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = sqlstatment;

                                //Insert the parameters first
                                cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
                                cmd.Parameters.AddWithValue("@FN", fnbox.Text);
                                cmd.Parameters.AddWithValue("@LN", lnamebox.Text);



                                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                                DataSet ds = new DataSet();
                                ad.SelectCommand = cmd;
                                ad.Fill(ds);
                                Session["UsrNme"] = UsrNme.Text;


                                }

                                Response.Redirect("User panel.aspx");
                    }

                }
                    else
                    {
                        Label1.Text = "please check the box to continue";
                    }

                }

            }

推荐答案


class ExtensionComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        string extensionX = Path.GetExtension(x);
        string extensionY = Path.GetExtension(y);
        return (extensionX == extensionY);
    }

    public int GetHashCode(string obj)
    {
        string extension = Path.GetExtension(obj.ToString());
        return extension.GetHashCode();
    }
}





只是一个例子来说明如何做。



Just a little example for how to do it.

string[] files = new string[] { "test.jpg", "test.png", "test.txt" };

string[] extensions = new string[] { ".jpg", ".png" };
bool success = (files.Intersect(extensions, new ExtensionComparer()).Count() > 0);



什么当你取得成功时,你所做的就取决于你。


What you do when you have a success is up to you.


这篇关于需要帮助来纠正我使用fileupload检查文件扩展名的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:40