Insert any file in DB from front end

Posted by Sanoop under ASP.NET on 12/12/2011 | Points: 10 | Views : 1231 | Status : [Member] | Replies : 2
Hi friends,
I want the code for saving any file in DB from front end using file upload tool




Responses

Posted by: Bees7000 on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
1- Data Base Table


CREATE TABLE tablename(

[ID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [varchar](50) NOT NULL,
[AttatchFile] [varbinary](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());

byte[] file;
BinaryReader br=new BinaryReader(FileUpload1.PostedFile.InputStream);
file=br.ReadBytes(FileUpload1.PostedFile.ContentLength);
cmd.Parameters.AddWithValue("@filename" , fileName);
cmd.Parameters.AddWithValue("@attatchfile" , file);
con.Open();
if (FileUpload1.HasFile)
{
cmd.ExecuteNonQuery();
}

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

}


Marwan

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

Posted by: Cnagasa on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
first create a table

CREATE TABLE [dbo].[T_ASNDOCS](
[T_ASNDocs_MdGodownSlno] [int] NULL,
[T_ASNDocs_TranType] [varchar](50) NOT NULL,
[T_ASNDocs_Slno] [int] IDENTITY(1,1) NOT NULL,
[T_ASNDocs_HdrSlno] [int] NULL,
[T_ASNDocs_filename] [image] NULL,
[T_ASNDocs_addtionaldesc] [varchar](255) NULL,
[T_ASNDocs_file] [varchar](255) NULL,
CONSTRAINT [PK_T_ASNDOCS] PRIMARY KEY CLUSTERED
(
[T_ASNDocs_Slno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

then design aspx page

<td><br />
Attach Documents:<label style="color:Red">*</label>
</td>
<td><br />
<asp:FileUpload ID="FileUpload1" Width="75%" BackColor="White" runat="server" />
</td>

coding of fileupload control:

string ext = string.Empty;
Boolean hita = false;
int i = fileName.Length - 1;
char[] arr = fileName.ToCharArray();
while (i > 0 & !hita)
{
if (arr[i] == '.') hita = true;
else ext = arr[i] + ext;
i = i - 1;
}
return ext;

private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case "htm":
case "html":
case "log":
return "text/HTML";
case "txt":
return "text/plain";
case "doc":
return "application/ms-word";
case "tiff":
case "tif":
return "image/tiff";
case "asf":
return "video/x-ms-asf";
case "avi":
return "video/avi";
case "zip":
return "application/zip";
case "xls":
case "csv":
return "application/vnd.ms-excel";
case "gif":
return "image/gif";
case "jpg":
case "jpeg":
return "image/jpeg";
case "bmp":
return "image/bmp";
case "wav":
return "audio/wav";
case "mp3":
return "audio/mpeg3";
case "mpg":
case "mpeg":
return "video/mpeg";
case "rtf":
return "application/rtf";
case "asp":
return "text/asp";
case "pdf":
return "application/pdf";
case "fdf":
return "application/vnd.fdf";
case "ppt":
return "application/mspowerpoint";
case "dwg":
return "image/vnd.dwg";
case "msg":
return "application/msoutlook";
case "xml":
case "sdxl":
return "application/xml";
case "xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}

Regard"s
Sudarshan.C

sudarshan.c

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

Login to post response