storing and retreiving documents from database

Posted by Saritha.Rajeshkumar under C# on 8/29/2013 | Points: 10 | Views : 1616 | Status : [Member] | Replies : 4
Hi Sir/Madam,

Iam Saritha, i have uploded a document called ("application.docx") in a database when am trying to retreive iam able to retreive the the file with "application.dcox" but iam not able to retreive the content in that file in page load.

thank u for ur consideration and time.




Responses

Posted by: Bandi on: 8/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link.. The process is explained clearly
http://archive.aspsnippets.com/post/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASPNet.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: Satyapriyanayak on: 8/29/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.c-sharpcorner.com/uploadfile/krishnasarala/inserting-file-and-retrieving-it-from-sql-server-in-Asp-Net/
http://chiragrdarji.wordpress.com/2007/08/31/storing-and-retrieving-docpdfxls-files-in-sql-server/

If this post helps you mark it as answer
Thanks

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

Posted by: Allemahesh on: 8/29/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You see the below link:-

http://codeabout.wordpress.com/2012/03/02/saving-files-into-your-sql-database-with-c/

Happy Coding

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

Posted by: Bandi on: 8/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Table Structure
CREATE TABLE tblFiles (id INT Identity, Name VARCHAR(50), ContentType VARCHAR(50), Data VarBinary(MAX))


--.aspx Page
<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" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<br />

<asp:Button ID="ShowFile" runat="server" Text="Show" OnClick="ShowFile_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""
Font-Names = "Arial"></asp:Label>
</div>
</form>
</body>
</html>


--.aspx.cs Page
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Data;

namespace namespaceName
{
public partial class SaveFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{
// Read the file and convert it to Byte Array
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;

//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
}
if (contenttype != String.Empty)
{

Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);

//insert the file into database
string strQuery = "insert into tblFiles(Name, ContentType, Data)" +
" values (@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
= contenttype;
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Uploaded Successfully";
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File format not recognised." +
" Upload Image/Word/PDF/Excel formats";
}
}


private Boolean InsertUpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["connStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}

protected void ShowFile_Click(object sender, EventArgs e)
{
string strQuery = "SELECT Name, ContentType, Data FROM tblFiles WHERE id=1 "; // Pass Id number based on your requirement from page
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dataTab = GetData(cmd);
download(dataTab);
}
private DataTable GetData(SqlCommand cmd)
{

DataTable dt = new DataTable();

String strConnString = System.Configuration.ConfigurationManager

.ConnectionStrings["connStrings"].ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

SqlDataAdapter sda = new SqlDataAdapter();

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

try
{

con.Open();

sda.SelectCommand = cmd;

sda.Fill(dt);

return dt;

}

catch
{

return null;

}

finally
{

con.Close();

sda.Dispose();

con.Dispose();

}

}

private void download(DataTable dt)
{

Byte[] bytes = (Byte[])dt.Rows[0]["Data"];

Response.Buffer = true;

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = dt.Rows[0]["ContentType"].ToString();

Response.AddHeader("content-disposition", "attachment;filename="

+ dt.Rows[0]["Name"].ToString());

Response.

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response