How to save image in folder without using database and show in datalist in asp.net

Many time we think that how  to bind the image dynamically in data list,so in this article we will learn  how to save image in particular image with image name .

Design:-



Source Code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist .aspx.cs" Inherits="datalist_" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   
    <div>

        <asp:FileUpload ID="fileupload1" runat="server" />
        <asp:Button ID="btnsave" runat="server" Text="Upload" OnClick="btnsave_Click" />
    </div>
    <br />
    <br />
    <div>
        <asp:DataList ID="dlimage" runat="server" RepeatColumns="5" CellPadding="5">
            <ItemTemplate>
                <asp:Image Width="150" ID="img1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>'
                    runat="server" />
                <br />
                <asp:HyperLink ID="hperlinkimagename" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images/{0}") %>'
                    runat="server" />
            </ItemTemplate>
            <ItemStyle BorderColor="lightblue" BorderStyle="Outset" BorderWidth="1px" HorizontalAlign="Center"
                VerticalAlign="Bottom" />
        </asp:DataList>
    </div>
    </form>
</body>
</html>


Code Behind(C#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

public partial class datalist_ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dlbind();
        }
    }
    protected void dlbind()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
        }
        dlimage.DataSource = listItems;
        dlimage.DataBind();

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
        fileupload1.SaveAs(Server.MapPath("Images/" + filename));
        dlbind();
    }
}


Out Put:-



How to save image in folder without using database and show in datalist in asp.net How to save image in folder without using database and show in  datalist in asp.net Reviewed by NEERAJ SRIVASTAVA on 1:27:00 PM Rating: 5

No comments:

Powered by Blogger.