Click here to Skip to main content
16,015,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a page. I use to upload images. I want to add function that checks if the image end with asp;.gif or asp;.bmp or asp;.jpg it cannot be upload.
Here is code of the function thats already in the page. It only need some if functions can anyone help me with this:

protected void btnUpload_Click(object sender, System.EventArgs e)
{
        string newDir = "~/" + upfileto.MainClass.CurrentRoom.Folder;
			if(btnUpload.Text !="Edit")
			{				
				if(FileCtrl1.PostedFile.FileName.EndsWith(".bmp")
					||FileCtrl1.PostedFile.FileName.EndsWith(".jpg")
					||FileCtrl1.PostedFile.FileName.EndsWith(".gif"))
				{
                    if (ddlFolder.SelectedItem.Value == "Icons")
                    {
                       FileCtrl1.Value="p"+Convert.ToString(GetImageID())+".gif";
                    }
                    FileCtrl1.SaveAs(Page.Server.MapPath(upfileto.MainClass.CurrentRoom.Folder + @"\" + ddlFolder.SelectedItem.Value + @"\") + FileCtrl1.Value);
					Label1.Text=FileCtrl1.Value;
                  
				}
				else
				{
					Response.Write("<script>alert('File Type is not supported ("+FileCtrl1.PostedFile.FileName+")');history.back()<"+"/script>");
					Response.End();
					
				}
			}
			else
			{
				
				if(FileCtrl1.PostedFile.FileName!="")
				{
					if(FileCtrl1.PostedFile.FileName.EndsWith(".bmp")
						||FileCtrl1.PostedFile.FileName.EndsWith(".jpg")
						||FileCtrl1.PostedFile.FileName.EndsWith(".gif"))
					{
                    
                    FileCtrl1.PostedFile.SaveAs(Page.Server.MapPath(upfileto.MainClass.CurrentRoom.Folder + @"\" + ddlFolder.SelectedItem.Value + @"\") + Request.QueryString["FileName"].ToString() );
                        
                       
					}
					else
					{
						Response.Write("<script>alert('File Type is not supported ("+FileCtrl1.PostedFile.FileName+")');history.back()<"+"/script>");
						Response.End();
					}			
				}
				
			}
			Response.Redirect("Main.aspx?Section=UpFile&i=1&Folder="+ddlFolder.SelectedItem.Value , true);
}
Posted
Updated 28-Dec-10 3:14am
v4
Comments
Sandeep Mewara 28-Dec-10 9:05am    
Use PRE tags to format code part. It makes the question readable.

1 solution

If you're using same thing in your program more then twice then you should use Function there to reduce code and more clear understanding.

likewise you're using same thing to check the extension of uploaded file, you could use function in space, see following.

public bool ShouldUpload(string filename)
{
if(filename.EndsWith(".jpg") || filename.EndsWith(".png"))
return true;
}

if(ShouldUpload(Fileupload1.postedfile.filename))
{
//upload image here.
}
 
Share this answer
 
v2
Comments
akoyoon 28-Dec-10 9:31am    
i donot understand what you mean
MrBatra 28-Dec-10 9:40am    
What's you're not understanding, be specific.

In case you;re not understanding the code here's what it does;


The above code is checking whether the file or image ends with ".jpg" or ".png" (in your case you can change this to ".bmp") ; If its true then exit from function don't do anything else the function will execute the code to upload the image.

You need to write that code for uploading.
akoyoon 28-Dec-10 9:51am    
your code will not be useful in case someone try to upload image with name like hi.asp;.jpg for example

your code do exactly what the code i posted do but it doesn't solve the problem
Hiren solanki 29-Dec-10 0:12am    
Dear akoyoon, I've posted code which could help you to make function in checking the file extension, you could add .asp,.jpg and millions of extensions you want, I am not going to include everything for you. Otherwise I might understand you wrong so please make us clear on what you exactly want. ?

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