----------------form design-----------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="multiuploadsintodatabase.aspx.cs" Inherits="DataUPndDownload.multiuploadsintodatabase" %>
<!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" multiple="multiple" runat="server" />
<asp:Button ID="Button1" runat="server" Text="upload" onclick="Button1_Click" />
<hr />
<asp:GridView ID="gvFiles" CssClass="GridViewStyle"
AutoGenerateColumns="true" runat="server">
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("id", "getfilesbylayers.aspx?id={0}") %>'
Text="Download"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</div>
</form>
</body>
</html>
-----------------------------FOrm behind Code---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;
using BLL;
namespace DataUPndDownload
{
public partial class multiuploadsintodatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gvFiles.DataSource = new UplaodDataBLL().getfiles();
gvFiles.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count;i++ )
{
BEUploadData be = new BEUploadData();
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
// Due to the limit of the max for a int type, the largest file can be
// uploaded is 2147483647, which is very large anyway.
int size = file.ContentLength;
string name = file.FileName;
int position = name.LastIndexOf("\\");
name = name.Substring(position + 1);
string contentType = file.ContentType;
byte[] fileData = new byte[size];
file.InputStream.Read(fileData, 0, size);
be.Name = name;
be.Size = size;
be.ContentType = contentType;
be.Data = fileData;
new UplaodDataBLL().savadata(be);
Label1.Text = "saved successfully";
gvFiles.DataSource = new UplaodDataBLL().getfiles();
gvFiles.DataBind();
}
}
}
}
}
----------------------
---------------------------------------BE-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BE
{
public class BEUploadData
{
public int id{get;set;}
public string Name{get;set;}
public string ContentType{get;set;}
public int Size{get;set;}
public Byte[] Data{get;set;}
}
}
--------------------------------BLL----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE;
using DAL;
using System.Data;
namespace BLL
{
public class UplaodDataBLL
{
public List<BEUploadData> getfiles()
{
return new uploaddataDAL().getfiles();
}
public void savadata(BEUploadData e)
{
new uploaddataDAL().savadata(e);
}
public DataTable GetAFile(int id)
{
return new uploaddataDAL().GetAFile(id);
}
}
}
------------------------------------
----------------------------------------Dal--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BE;
namespace DAL
{
public class uploaddataDAL
{
private static string path = MyConnection.path;
public List<BEUploadData> getfiles()
{
List<BEUploadData>list=new List<BEUploadData>();
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_selectdata.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
BEUploadData be = new BEUploadData();
be.id = Convert.ToInt32(sdr["id"]);
be.Name = Convert.ToString(sdr["Name"]);
be.Size = Convert.ToInt32(sdr["Size"]);
be.ContentType = Convert.ToString(sdr["ContentType"]);
be.Data = (Byte[])(sdr["Data"]);
list.Add(be);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return list;
}
public DataTable GetAFile(int id)
{
DataTable file = new DataTable();
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_getfilebyID.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter da = new SqlDataAdapter();
try
{
con.Open();
da.SelectCommand = cmd;
da.Fill(file);
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return file;
}
public void savadata(BEUploadData e)
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_insertdata.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", e.Name);
cmd.Parameters.AddWithValue("@Size", e.Size);
cmd.Parameters.AddWithValue("@ContentType", e.ContentType);
cmd.Parameters.AddWithValue("@Data", e.Data);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
----------------------dalMYporc--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DAL
{
public class MyProc
{
public enum name
{
sp_insertdata,
sp_selectdata,
sp_getfilebyID
}
}
}
--------------------------Dal MyConnection----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace DAL
{
public class MyConnection
{
public static string path;
static MyConnection()
{
path = ConfigurationManager.ConnectionStrings["gn"].ConnectionString;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="multiuploadsintodatabase.aspx.cs" Inherits="DataUPndDownload.multiuploadsintodatabase" %>
<!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" multiple="multiple" runat="server" />
<asp:Button ID="Button1" runat="server" Text="upload" onclick="Button1_Click" />
<hr />
<asp:GridView ID="gvFiles" CssClass="GridViewStyle"
AutoGenerateColumns="true" runat="server">
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("id", "getfilesbylayers.aspx?id={0}") %>'
Text="Download"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</div>
</form>
</body>
</html>
-----------------------------FOrm behind Code---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;
using BLL;
namespace DataUPndDownload
{
public partial class multiuploadsintodatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gvFiles.DataSource = new UplaodDataBLL().getfiles();
gvFiles.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count;i++ )
{
BEUploadData be = new BEUploadData();
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
// Due to the limit of the max for a int type, the largest file can be
// uploaded is 2147483647, which is very large anyway.
int size = file.ContentLength;
string name = file.FileName;
int position = name.LastIndexOf("\\");
name = name.Substring(position + 1);
string contentType = file.ContentType;
byte[] fileData = new byte[size];
file.InputStream.Read(fileData, 0, size);
be.Name = name;
be.Size = size;
be.ContentType = contentType;
be.Data = fileData;
new UplaodDataBLL().savadata(be);
Label1.Text = "saved successfully";
gvFiles.DataSource = new UplaodDataBLL().getfiles();
gvFiles.DataBind();
}
}
}
}
}
----------------------
---------------------------------------BE-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BE
{
public class BEUploadData
{
public int id{get;set;}
public string Name{get;set;}
public string ContentType{get;set;}
public int Size{get;set;}
public Byte[] Data{get;set;}
}
}
--------------------------------BLL----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE;
using DAL;
using System.Data;
namespace BLL
{
public class UplaodDataBLL
{
public List<BEUploadData> getfiles()
{
return new uploaddataDAL().getfiles();
}
public void savadata(BEUploadData e)
{
new uploaddataDAL().savadata(e);
}
public DataTable GetAFile(int id)
{
return new uploaddataDAL().GetAFile(id);
}
}
}
------------------------------------
----------------------------------------Dal--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BE;
namespace DAL
{
public class uploaddataDAL
{
private static string path = MyConnection.path;
public List<BEUploadData> getfiles()
{
List<BEUploadData>list=new List<BEUploadData>();
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_selectdata.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
BEUploadData be = new BEUploadData();
be.id = Convert.ToInt32(sdr["id"]);
be.Name = Convert.ToString(sdr["Name"]);
be.Size = Convert.ToInt32(sdr["Size"]);
be.ContentType = Convert.ToString(sdr["ContentType"]);
be.Data = (Byte[])(sdr["Data"]);
list.Add(be);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return list;
}
public DataTable GetAFile(int id)
{
DataTable file = new DataTable();
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_getfilebyID.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter da = new SqlDataAdapter();
try
{
con.Open();
da.SelectCommand = cmd;
da.Fill(file);
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return file;
}
public void savadata(BEUploadData e)
{
SqlConnection con = new SqlConnection(path);
SqlCommand cmd = new SqlCommand(MyProc.name.sp_insertdata.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", e.Name);
cmd.Parameters.AddWithValue("@Size", e.Size);
cmd.Parameters.AddWithValue("@ContentType", e.ContentType);
cmd.Parameters.AddWithValue("@Data", e.Data);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
----------------------dalMYporc--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DAL
{
public class MyProc
{
public enum name
{
sp_insertdata,
sp_selectdata,
sp_getfilebyID
}
}
}
--------------------------Dal MyConnection----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace DAL
{
public class MyConnection
{
public static string path;
static MyConnection()
{
path = ConfigurationManager.ConnectionStrings["gn"].ConnectionString;
}
}
}
No comments:
Post a Comment