Wednesday 3 April 2013

How to Upload file into Database and Display it using Gridview and Download the File


<!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="Btn_Upload" runat="server" Text="Upload" OnClick="Btn_Upload_Click" />
        <asp:GridView ID="Grdvdetails" runat="server" AutoGenerateColumns="false" DataKeyNames="FilePath">
            <HeaderStyle BackColor="BlueViolet" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="FileName" HeaderText="FileName" />
                <asp:TemplateField HeaderStyle-BackColor="Chocolate">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkdownload" runat="server" Text="Download" OnClick="lnkdownload_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

In the Code behind write the Following Code
------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class DownloadUpload : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgridview();
        }
    }
    void fillgridview()
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Files", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        con.Close();
        Grdvdetails.DataSource = ds;
        Grdvdetails.DataBind();


    }
    protected void Btn_Upload_Click(object sender, EventArgs e)
    {
        string filename = FileUpload1.PostedFile.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Files/" + filename));

        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Files(FileName,FilePath) values(@FileName,@FilePath)", con);
        cmd.Parameters.AddWithValue("@FileName", filename);
        cmd.Parameters.AddWithValue("@FilePath", "Files/" + filename);
        cmd.ExecuteNonQuery();
        fillgridview();
        con.Close();
    }
    protected void lnkdownload_Click(object sender, EventArgs e)
    {
        LinkButton lnk = sender as  LinkButton ;
        GridViewRow gvrow = (GridViewRow)lnk.NamingContainer;
        string filepath = Grdvdetails.DataKeys[gvrow.RowIndex].Value.ToString();
        Response.ContentType = "image/jpg";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filepath + "\"");
        Response.TransmitFile(Server.MapPath(filepath));
        Response.End();

    }
}

No comments:

Post a Comment