Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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
Posted
Updated 10-Jul-13 14:07pm
v4

There is no "OR" operator. Should be:
C#
if ((filext == ".png") || (filext == ".jpeg"))
    // ...

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:
C#
loExt = fileExt.ToLower();
if ((loExt == ".png") || (loExt == ".jpeg") || (loExt == ".jpg"))
   //...


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
 
Share this answer
 
Comments
Sugu Thomas 10-Jul-13 19:25pm    
Thank you @sergey....valuable information!!
Sergey Alexandrovich Kryukov 10-Jul-13 19:35pm    
You are very welcome.
Good luck, call again.
—SA
Instead of
Quote:
if (filext == ".png") || (filext == ".jpeg")
try
if (filext == ".png" || filext == ".jpeg")
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jul-13 19:34pm    
That's correct, but, if you look at my answer, you will see that it still does not resolve the practical problem. Different casing should be taken into account...
—SA

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