Hi @Gani_tpt,
Based on your needs, I tested an example, hope it can help you.
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:Button ID="AddUsers" runat="server" Text="Insert" OnClick="Insert" />
<hr />
<asp:GridView runat="server" ID="gvFiles" AutoGenerateColumns="false" AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true" OnRowCancelingEdit="OnRowCancelingEdit" OnRowDeleting="OnRowDeleting"
OnRowEditing="OnRowEditing" OnRowUpdating="OnRowUpdating" DataKeyNames="Id">
<Columns>
<asp:BoundField DataField="Name" HeaderText="File Name"></asp:BoundField>
<asp:TemplateField HeaderText="Files">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fuFile" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGridView();
}
}
protected void Insert(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload2.PostedFile.FileName);
string contentType = FileUpload2.PostedFile.ContentType;
using (Stream fs = FileUpload2.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO tblFiles(Name,ContentType,Data) VALUES(@Name,@ContentType,@Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
}
}
protected void OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvFiles.EditIndex = -1;
BindGridView();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
gvFiles.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvFiles.Rows[e.RowIndex];
int id = Convert.ToInt32(gvFiles.DataKeys[e.RowIndex].Values[0]);
FileUpload FileUpload1 = row.FindControl("fuFile") as FileUpload;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = "";
byte[] bytes = null;
if (FileUpload1.HasFile)
{
contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
using (SqlConnection con = new SqlConnection(str))
{
string query = "UPDATE tblFiles SET Name = @Name,ContentType = @ContentType,Data=@Data WHERE id = @Id";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes == null ? new byte[] { } : bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
gvFiles.EditIndex = -1;
BindGridView();
}
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Name, Data, ContentType from tblFiles where Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
private void BindGridView()
{
using (SqlConnection con = new SqlConnection(str))
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblFiles", con))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
gvFiles.DataSource = dt;
gvFiles.DataBind();
}
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.