需要帮助来纠正我使用fileupload检查文件扩展名的代码 [英] Need assistance to correct my code of checking file extension with fileupload

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

问题描述


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

Hi the 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";
                    }

                }

            }

推荐答案

如果你有代码在下面某行,然后执行肯定。



如果您不想执行以下代码,那么您需要返回或设计您的活动以这种方式,该行将是最后一行代码。
If you have codes below to some line, then that would execute for sure.

If you don't want to execute the below code, then you need to return or design your event in that way, where that line would be the last line of code.


您无法将整个文件名与扩展名进行比较,test.jpg将永远不会等于.jpg。

所以你需要制作自己的比较对象,考虑到这一点。

请参阅下面的小例子。



您需要创建自己的继承自 IEqualityComparer 的类并实现这两种方法。

You cannot compare the whole file name with an extension, test.jpg will never be equal to .jpg.
So you need to make your own compare object, where you take that into consideration.
See my small example below.

You need to create your own class that inherits from IEqualityComparer and implement the two methods.
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检查文件扩展名的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆