Upload and display in folders as per catagory in ASP.Net

Last Reply on Apr 14, 2014 07:05 AM By Mudassar

Posted on Apr 14, 2014 07:05 AM

 

I want to develop image album on my website

1 upload photo category wise

2 view photo category wise

 

 

 

You are viewing reply posted by: Mudassar on Apr 14, 2014 07:05 AM.
Posted on Apr 14, 2014 07:05 AM

This way

 HTML:

<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Categories_OnSelectedIndexChanged">
            <asp:ListItem Text="Please Select" Value="0" />
            <asp:ListItem Text="Animal" Value="Animal" />
            <asp:ListItem Text="Flower" Value="Flower" />
        </asp:DropDownList>
        <br />
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
        <hr />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
            <Columns>
                <asp:BoundField DataField="Text" />
                <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
            </Columns>
        </asp:GridView>
    </div>
</form>

C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.PopulateGridView(this.ddlCategories.SelectedItem.Value);
    }
}

protected void Upload(object sender, EventArgs e)
{
    if (this.FileUpload1.HasFile)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("~/Images/" + this.ddlCategories.SelectedItem.Value));
        if (!directoryInfo.Exists)
        {
            directoryInfo.Create();
        }

        this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/" + this.ddlCategories.SelectedItem.Value + "/") + FileUpload1.FileName);
        this.PopulateGridView(this.ddlCategories.SelectedItem.Value);
    }
}

protected void Categories_OnSelectedIndexChanged(object sender, EventArgs e)
{
    this.PopulateGridView(this.ddlCategories.SelectedItem.Value);
}

private void PopulateGridView(string category)
{
    if (category != "0")
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("~/Images/" + this.ddlCategories.SelectedItem.Value));
        if (directoryInfo.Exists)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/" + category));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                string fileName = Path.GetFileName(filePath);
                files.Add(new ListItem(fileName, "~/Images/" + category + "/" + fileName));
            }
            this.GridView1.DataSource = files;
            this.GridView1.DataBind();
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('No Such Directory Exists. Please Upload The File')", true);
        }
    }
}

Image: