Hi sofia,
Please refer below Sample.
HTML
Default
<table>
<tr>
<td colspan="2"><asp:FileUpload runat="server" ID="fuUserImage" /></td>
</tr>
<tr>
<td>File Name:</td>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td><asp:Button Text="Save" ID="btnSave" runat="server" OnClick="Save" /></td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="gvUsers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FileName" HeaderText="FileName" />
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<img src="<%# Eval("FilePath") %>" height="100" width="100" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<hr />
<asp:Button Text="Send" ID="btnSend" runat="server" OnClick="Send" />
Home
<asp:Repeater runat="server" ID="rptUsers" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<table>
<tr>
<td><asp:Image ID="imgUser" runat="server" ImageUrl='<%# Eval("FilePath") %>' Height="100" Width="100" />
<br />
<br />
<asp:FileUpload runat="server" ID="fuImage" Visible="false" />
<br />
<br />
<asp:Button runat="server" ID="btnUpload" Text="Upload" Visible="false" /></td>
<td><asp:Label runat="server" Text='<%# Eval("FileName") %>'></asp:Label></td>
</tr>
</table>
<hr />
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download File" OnClick="DownloadFile"
CommandArgument='<%# Eval("FilePath") %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("FileName"),
new DataColumn("FilePath")
});
ViewState["Users"] = dt;
this.BindGrid();
}
}
protected void Save(object sender, EventArgs e)
{
string filePath = string.Empty;
if (fuUserImage.HasFile)
{
filePath = "Images/" + Path.GetFileName(fuUserImage.PostedFile.FileName);
fuUserImage.SaveAs(Server.MapPath("~/" + filePath));
}
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblfiles (FileName, filePath) Values (@Name, @Path)", con))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Path", filePath);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
DataTable dt = (DataTable)ViewState["Users"];
dt.Rows.Add(txtName.Text.Trim(), filePath);
ViewState["Users"] = dt;
this.BindGrid();
txtName.Text = string.Empty;
}
protected void Send(object sender, EventArgs e)
{
Session["Users"] = ViewState["Users"];
Response.Redirect("~/Home.aspx");
}
protected void BindGrid()
{
gvUsers.DataSource = (DataTable)ViewState["Users"];
gvUsers.DataBind();
}
Home
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = (DataTable)Session["Users"];
rptUsers.DataSource = dt;
rptUsers.DataBind();
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
byte[] bytes = File.ReadAllBytes(Server.MapPath("~/" + filePath));
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image imgUser = e.Item.FindControl("imgUser") as Image;
if (string.IsNullOrEmpty(imgUser.ImageUrl))
{
(e.Item.FindControl("fuImage") as FileUpload).Visible = true;
(e.Item.FindControl("btnUpload") as Button).Visible = true;
(e.Item.FindControl("lnkDownload") as LinkButton).Visible = false;
}
}
}
protected void Upload(object sender, EventArgs e)
{
// Code for upload the file and update the record in database.
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("FileName"), New DataColumn("FilePath")})
ViewState("Users") = dt
Me.BindGrid()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = String.Empty
If fuUserImage.HasFile Then
filePath = "Images/" & Path.GetFileName(fuUserImage.PostedFile.FileName)
fuUserImage.SaveAs(Server.MapPath("~/" & filePath))
End If
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblfiles (FileName, filePath) Values (@Name, @Path)", con)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Path", filePath)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Dim dt As DataTable = CType(ViewState("Users"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), filePath)
ViewState("Users") = dt
Me.BindGrid()
txtName.Text = String.Empty
End Sub
Protected Sub Send(ByVal sender As Object, ByVal e As EventArgs)
Session("Users") = ViewState("Users")
Response.Redirect("~/Home.aspx")
End Sub
Protected Sub BindGrid()
gvUsers.DataSource = CType(ViewState("Users"), DataTable)
gvUsers.DataBind()
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = CType(Session("Users"), DataTable)
rptUsers.DataSource = dt
rptUsers.DataBind()
End Sub
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = (TryCast(sender, LinkButton)).CommandArgument
Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/" & filePath))
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "image/jpg"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(filePath))
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim imgUser As Image = TryCast(e.Item.FindControl("imgUser"), Image)
If String.IsNullOrEmpty(imgUser.ImageUrl) Then
TryCast(e.Item.FindControl("fuImage"), FileUpload).Visible = True
TryCast(e.Item.FindControl("btnUpload"), Button).Visible = True
TryCast(e.Item.FindControl("lnkDownload"), LinkButton).Visible = False
End If
End If
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
''Code for upload the file and update the record in database.
End Sub