我可以给||吗而不是使用"OR"?我收到错误1:无效的表达式项'||' [英] Can i give || instead of using 'OR'? I am getting Error1 :Invalid expression term '||'

查看:103
本文介绍了我可以给||吗而不是使用"OR"?我收到错误1:无效的表达式项'||'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Cmdupload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filext = System.IO.Path.GetExtension(FileUpload1.FileName);
              // Here Problem! if (filext == ".png") || (filext == ".jpeg")
                {
                    try
                    {
                        _Imgname = Path.GetFileName(FileUpload1.PostedFile.FileName);
                        FileUpload1.SaveAs(Server.MapPath("Prdo_Images/" + _Imgname));
                        _ImgPath = "Prdo_Images/" + FileUpload1.FileName;
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('File Uploaded Succesfully');", true);
                    }
                    catch (Exception ex)
                    {
                         ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Error ')" + ex.Message + ";", true);
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Only png or Jpeg files allowed!');", true);

                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('You have not specified file.');", true);
            }

        }





请帮忙!

在此先感谢





Please help!!

Thanks in advance

推荐答案

没有或"运算符.应该是:
There is no "OR" operator. Should be:
if ((filext == ".png") || (filext == ".jpeg"))
    // ...


您看得出来差别吗? :-)

此外,此检查也不是一件好事,因为它错过了使用"* .PnG","*.JPG","*.JPg"等正确名称的可能性.您实际上不需要使用它,因为您可以使用文件对话框过滤器.但是,如果您真的需要检查扩展名",则可能是这样的:


Can you spot the difference? :-)

Besides, this check is no good, because it misses the possibility of correct names with "*.PnG", "*.JPG", "*.JPg" and so on. You don''t really need to use it, as you can use the file dialog filter. But if you think you really need to checkup "extension", it could be something like:

loExt = fileExt.ToLower();
if ((loExt == ".png") || (loExt == ".jpeg") || (loExt == ".jpg"))
   //...



请参阅: http://msdn.microsoft.com/en-us/library/system .string.tolower.aspx [ ^ ].

最后,即使在几乎所有情况下都需要使用"||",您也应该清楚地了解"|"和"||"运算符之间的区别:
http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/6373h346.aspx [ ^ ].

-SA



Please see: http://msdn.microsoft.com/en-us/library/system.string.tolower.aspx[^].

And finally, even though you need to use ''||'' in almost all cases, you should clearly understand the difference between ''|'' and ''||'' operators:
http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx[^],
http://msdn.microsoft.com/en-us/library/6373h346.aspx[^].

—SA


而不是
引用:

if(filext ==".png)|| (filext ==".jpeg")

if (filext == ".png") || (filext == ".jpeg")

尝试

if (filext == ".png" || filext == ".jpeg")


这篇关于我可以给||吗而不是使用"OR"?我收到错误1:无效的表达式项'||'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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