Upload and Save Or Store Files In Ms Sql Server Database using fileupload control in Asp.Net.
In this post i'm explaining how to save or store and retrieve pdf,word,excel,jpeg,gif,png files in MS sqlserver database using fileupload in asp.net.
I'll also explain how to retrieve files from sql database for users to download.
For this example i have created a sample database with table name SaveDoc to save documents in it. table schema is shown below in image.
Read Display Images In GridView From SqlServer DataBase Asp.Net to know how to save and retrieve images in sql database and display them in gridview using handler.
For uploading and saving files to database we need to use Fileupload control, so drag and place one fileupload control and one button on the aspx page in design mode.
Place one label on the page to display success or failure message, and one gridview to display uploaded documents and provide link to download document files.
I have added one button field in gridview to provide download link to file shown in respective row of gridview and this gridview is populated by sqlDataSource.
To upload and save files in database write code mentioned below in Click event of upload button we placed on aspx page.
To retrieve files from database for download in click of download button we put in gridview, we need to write code mentioned below in RowCommand Event of gridview
Build and run the application.
In this post i'm explaining how to save or store and retrieve pdf,word,excel,jpeg,gif,png files in MS sqlserver database using fileupload in asp.net.
I'll also explain how to retrieve files from sql database for users to download.
For this example i have created a sample database with table name SaveDoc to save documents in it. table schema is shown below in image.
Read Display Images In GridView From SqlServer DataBase Asp.Net to know how to save and retrieve images in sql database and display them in gridview using handler.
For uploading and saving files to database we need to use Fileupload control, so drag and place one fileupload control and one button on the aspx page in design mode.
Place one label on the page to display success or failure message, and one gridview to display uploaded documents and provide link to download document files.
I have added one button field in gridview to provide download link to file shown in respective row of gridview and this gridview is populated by sqlDataSource.
HTML SOURCE OF THE PAGE
<form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server"/> <asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click" Text="Upload"/> </div> <br/> <asp:Label ID="lblMessage" runat="server"> </asp:Label><br /><br /><br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand" DataKeyNames="DocID"> <Columns> <asp:BoundField DataField="DocID" HeaderText="DocID" InsertVisible="False" ReadOnly="True" SortExpression="DocID" /> <asp:BoundField DataField="DocName" HeaderText="DocName" SortExpression="DocName" /> <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> <asp:ButtonField ButtonType="Image" ImageUrl="~/download.png" CommandName="Download" HeaderText="Download" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [DocID], [DocName], [Type] FROM [SaveDoc]"> </asp:SqlDataSource> </form>
To upload and save files in database write code mentioned below in Click event of upload button we placed on aspx page.
C# CODE
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void btnUpload_Click(object sender, EventArgs e)
{
//Check whether FileUpload control has file
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string documentType = string.Empty;
//provide document type based on it's extension
switch (fileExtension)
{
case ".pdf":
documentType = "application/pdf";
break;
case ".xls":
documentType = "application/vnd.ms-excel";
break;
case ".xlsx":
documentType = "application/vnd.ms-excel";
break;
case ".doc":
documentType = "application/vnd.ms-word";
break;
case ".docx":
documentType = "application/vnd.ms-word";
break;
case ".gif":
documentType = "image/gif";
break;
case ".png":
documentType = "image/png";
break;
case ".jpg":
documentType = "image/jpg";
break;
}
//Calculate size of file to be uploaded
int fileSize = FileUpload1.PostedFile.ContentLength;
//Create array and read the file into it
byte[] documentBinary = new byte[fileSize];
FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);
// Create SQL Connection
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// Create SQL Command and Sql Parameters
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" +
" VALUES (@DocName,@Type,@DocData)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
DocName.Value = fileName.ToString();
cmd.Parameters.Add(DocName);
SqlParameter Type = new SqlParameter("@Type", SqlDbType.VarChar, 50);
Type.Value = documentType.ToString();
cmd.Parameters.Add(Type);
SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary,fileSize);
uploadedDocument.Value = documentBinary;
cmd.Parameters.Add(uploadedDocument);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblMessage.Text = "File saved to database";
GridView1.DataBind();
}
}
VB.NET CODE
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
'Check whether FileUpload control has file
If FileUpload1.HasFile Then
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim fileExtension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim documentType As String = String.Empty
'provide document type based on it's extension
Select Case fileExtension
Case ".pdf"
documentType = "application/pdf"
Exit Select
Case ".xls"
documentType = "application/vnd.ms-excel"
Exit Select
Case ".xlsx"
documentType = "application/vnd.ms-excel"
Exit Select
Case ".doc"
documentType = "application/vnd.ms-word"
Exit Select
Case ".docx"
documentType = "application/vnd.ms-word"
Exit Select
Case ".gif"
documentType = "image/gif"
Exit Select
Case ".png"
documentType = "image/png"
Exit Select
Case ".jpg"
documentType = "image/jpg"
Exit Select
End Select
'Calculate size of file to be uploaded
Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
'Create array and read the file into it
Dim documentBinary As Byte() = New Byte(fileSize - 1) {}
FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize)
' Create SQL Connection
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
' Create SQL Command and Sql Parameters
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" & " VALUES (@DocName,@Type,@DocData)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim DocName As New SqlParameter("@DocName", SqlDbType.VarChar, 50)
DocName.Value = fileName.ToString()
cmd.Parameters.Add(DocName)
Dim Type As New SqlParameter("@Type", SqlDbType.VarChar, 50)
Type.Value = documentType.ToString()
cmd.Parameters.Add(Type)
Dim uploadedDocument As New SqlParameter("@DocData", SqlDbType.Binary, fileSize)
uploadedDocument.Value = documentBinary
cmd.Parameters.Add(uploadedDocument)
con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
con.Close()
If result > 0 Then
lblMessage.Text = "File saved to database"
End If
GridView1.DataBind()
End If
End SubTo retrieve files from database for download in click of download button we put in gridview, we need to write code mentioned below in RowCommand Event of gridview
c# CODE
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
string fileName = string.Empty;
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
int documentID = Convert.ToInt32(GridView1.DataKeys[index].Value);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT DocName,DocData FROM SaveDoc WHERE DocID = " + documentID, con);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
while (dReader.Read())
{
fileName = dReader["DocName"].ToString();
byte[] documentBinary = (byte[])dReader["DocData"];
FileStream fStream = new FileStream(Server.MapPath("Docs") + @"\" + fileName, FileMode.Create);
fStream.Write(documentBinary, 0, documentBinary.Length);
fStream.Close();
fStream.Dispose();
}
con.Close();
Response.Redirect(@"Docs\" + fileName);
}
}VB.NET CODE
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Download" Then
Dim fileName As String = String.Empty
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)
Dim documentID As Integer = Convert.ToInt32(GridView1.DataKeys(index).Value)
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim cmd As New SqlCommand("SELECT DocName,DocData FROM SaveDoc WHERE DocID = " & documentID, con)
con.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
While dReader.Read()
fileName = dReader("DocName").ToString()
Dim documentBinary As Byte() = DirectCast(dReader("DocData"), Byte())
Dim fStream As New FileStream(Server.MapPath("Docs") & "\" & fileName, FileMode.Create)
fStream.Write(documentBinary, 0, documentBinary.Length)
fStream.Close()
fStream.Dispose()
End While
con.Close()
Response.Redirect("Docs\" & fileName)
End If
End SubBuild and run the application.
If you like this post than join us or share


0 comments:
Post a Comment