save/upload files in folder and download files from folder in asp.net
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Save and Download Files from file system</title>
<style type="text/css">
.modalBackground
{
background-color: Gray;
filter: alpha(opacity=80);
opacity: 0.8;
z-index: 10000;
}
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center}
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color: White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileUpload1" runat="server" /><br /><br />
Date: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
Title: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />
</div>
<div>
<asp:GridView ID="gvDetails" CssClass="Gridview" runat="server"
AutoGenerateColumns="false" DataKeyNames="FilePath"
onrowdatabound="gvDetails_RowDataBound">
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<%--<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>--%>
<asp:ImageButton ID="lnkDownload" runat="server"
ImageUrl="~/Images/arrow_down.png" onclick="lnkDownload_Click1"
style="width: 20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
/////////////////////////////////////
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
private SqlConnection con = new SqlConnection("Data Source=DIVAKAR-PC\\SQLEXPRESS;Initial Catalog=DPAI;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridviewData();
}
}
// Bind Gridview Data
private void BindGridviewData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from FilesTable",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
// Save files to Folder and files path in database
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("Files/"+filename));
con.Open();
SqlCommand cmd = new SqlCommand("insert into FilesTable(Date,Title,FilePath) values(@Date,@Title,@Path)", con);
// cmd.Parameters.AddWithValue("@Name",filename );
cmd.Parameters.AddWithValue("@Date", TextBox1.Text.ToString());
cmd.Parameters.AddWithValue("@Title", TextBox2.Text.ToString());
cmd.Parameters.AddWithValue("@Path", "Files/"+filename );
cmd.ExecuteNonQuery();
con.Close();
BindGridviewData();
}
protected void lnkDownload_Click1(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton lnkbtn = sender as ImageButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
i++;
string s = cell.Text;
if (cell.Text.Length > 25 && (i == 3))
cell.Text = cell.Text.Substring(0, 50) + "....";
cell.ToolTip = s;
}
}
}
}
No comments:
Post a Comment