Saving a file in DB from front end

Posted by Sanoop under ASP.NET on 12/13/2011 | Points: 10 | Views : 1138 | Status : [Member] | Replies : 2
Hi friends
When a file is uploaded using file uploader tool. Only the path have to saved in the DB not the entire file.
Can anyone send the code for this




Responses

Posted by: Sksamantaray on: 12/13/2011 [Member] Silver | Points: 25

Up
0
Down
string UploadFileName = FileUpload1.FileName.Trim();
save this to database
at the same time copy the file to physical folder
protected void UploadFile()
{
string CurrentFileName;
string ExtFile;

CurrentFileName = FileUpload1.FileName;
ExtFile = Path.GetExtension(CurrentFileName);

if (ExtFile != ".zip")
{
///Call Ajax Popuup Extender
}
if (this.FileUpload1.PostedFile.ContentLength > 131072)
{
//show some message
}

string CurrentPath = Server.MapPath("~/uploadFiles/");

if (this.FileUpload1.HasFile)
{
CurrentPath += CurrentFileName;
FileUpload1.SaveAs(CurrentPath);



}
else
{
Response.Write("Can not load the file !");
}


}

Thanks,
Sanjay

Sanoop, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/13/2011 [Member] Bronze | Points: 25

Up
0
Down

CREATE TABLE tablename(
[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[AttatchFile] [varchar](max) NOT NULL,
CONSTRAINT pk_tablename PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


2-Aspx Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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>
</div>
<div>
File:
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="SaveBtn" runat="server" Height="40px" OnClick="Button1_Click" Text="Save"
Width="90px" /></div>
</form>
</body>
</html>


3-server side


 

protected void Button1_Click(object sender , EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=YourServer-pc;Initial Catalog=YourDataBase;Integrated Security=True");
SqlCommand cmd=new SqlCommand("insert into attatchments(filename,attatchfile)values(@filename,@attatchfile)" , con);

string fileName=FileUpload1.PostedFile.FileName.ToString();

FileInfo info=new FileInfo(FileUpload1.PostedFile.FileName.ToString());

string file;
StreamReader br=new StreamReader(FileUpload1.PostedFile.InputStream);
file=br.ReadToEnd();
cmd.Parameters.AddWithValue("@filename" , fileName);
cmd.Parameters.AddWithValue("@attatchfile" , file);
con.Open();
if (FileUpload1.HasFile)
{
cmd.ExecuteNonQuery();
}

cmd.Dispose();
con.Close();

}



Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Sanoop, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response