Friday 15 June 2012

File upload in Database and File show in a GridView And Download the Word File


Page Design
Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DownloadFilein asp.aspx.cs" Inherits="DownloadFilein_asp" %>

<!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="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowcommand="GridView1_RowCommand" >
        <Columns>
         <asp:BoundField HeaderText="ID" DataField="ID" />
        <asp:BoundField HeaderText="FileName" DataField="FileName" />
        <asp:ButtonField ButtonType="Link" Text="Download" CommandName="Dwn" HeaderText="Files" />
        </Columns>
        </asp:GridView>
       
    </div>
    </form>
</body>
</html>

C# Code
Add a Folder in the Website which name is Docs
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.Configuration;
using System.IO;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class DownloadFilein_asp : System.Web.UI.Page

{
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Satya"].ConnectionString);
        con.Open();
        if (!IsPostBack)
        {
            Fillgrid();
        }
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string name = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string location = Server.MapPath("~/Docs/" + name);
            FileUpload1.SaveAs(location);

            string connectionString = ConfigurationManager.ConnectionStrings["Satya"].ConnectionString;
            SqlConnection sqlCon = new SqlConnection(connectionString);
            string strInsert = "INSERT INTO Files(FileName) VALUES(@FileName)";
            SqlCommand command = new SqlCommand(strInsert, sqlCon);
            command.Parameters.AddWithValue("@FileName", name);
            sqlCon.Open();
            int result = command.ExecuteNonQuery();
            sqlCon.Close();

            if (result > 0)
                lblMessage.Text = "Upload Successful";
        }
        Fillgrid();

    }
    public void Fillgrid()
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from Files", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {       
      if (e.CommandName == "Dwn")
      {
          int index = Convert.ToInt32(e.CommandArgument);
           GridViewRow row = GridView1.Rows[index];
           string fName = row.Cells[1].Text;
           Response.ContentType = "application/octet-stream";     
          Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
           Response.TransmitFile(Server.MapPath("~/Docs/" + fName));
           Response.End();
       }
    }
}

Database Design 

No comments:

Post a Comment