Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I am making an image gallery for a website using C#. It should save, retrieve and delete the images from a source folder. It already uploads and retrieves the images but I do not know what commands to use for selecting then deleting the image. Could anyone show or direct me to the right direction?
Edit: the images are shown in a panel too.

ASP.NET
<div>
    <asp:FileUpload ID="FileUp1" runat="server" /> <asp:Button ID="BtnUP"
        runat="server" Text="Upload" onclick="BtnUP_Click" />
    <asp:Panel ID="PnlPic1" runat="server" BorderColor="Pink" BorderStyle="Double"
        BorderWidth="5px" Width="500px">
    </asp:Panel>
</div>


C#
protected void Page_Load(object sender, EventArgs e)
{
    UploadImage();
}
protected void BtnUP_Click(object sender, EventArgs e)
{
    if (FileUp1.HasFile)
    {
        string fileName = FileUp1.FileName;
        FileUp1.PostedFile.SaveAs(Server.MapPath("~/Data/" + fileName));

    }
    Response.Redirect("~/Default.aspx");

}

private void UploadImage()
{
    foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/Data")))
    {
        ImageButton imgbtn = new ImageButton();
        FileInfo finfo = new FileInfo(strFileName);
        imgbtn.ImageUrl = "~/Data/" + finfo.Name;
        imgbtn.Width = Unit.Pixel(100);
        imgbtn.Height = Unit.Pixel(100);
        imgbtn.Style.Add("padding", "5px");
        imgbtn.Click += new ImageClickEventHandler(imgbtn_Click);
        PnlPic1.Controls.Add(imgbtn);
    }
}
void imgbtn_Click(object sender, ImageClickEventArgs e)
{
    Response.Redirect(((imgbtn)sender).ImageUrl);
}
Posted
Updated 15-Nov-13 2:56am
v2
Comments
ZurdoDev 15-Nov-13 9:10am    
You get to decide how you want it to work. Perhaps a Repeater control, or some jquery slideshow type of control. Or even a drop down list. It's up to you how you give them the option of deleting the images.

1 solution

this is to store image in source folder
C#
if (FileUp1.HasFile)
        {
            string fileName = FileUp1.FileName;
            FileUp1.PostedFile.SaveAs(Server.MapPath( fileName));

        }



this is to delete the image in source folder
get the file name from daatabase and store in delimage as variable
C#
if (delImage != "" && delImage != null)
                               {
                                   System.IO.File.Delete(Request.PhysicalApplicationPath + "Data/" + delImage);
                               }
 
Share this answer
 
Comments
Lin Jiamei 1-Dec-13 9:51am    
Is there a way to make this work without using a database?

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