Friday, May 6, 2011

How to Save a file into database and display on the user browser


Here we will learn how to save a file into sqlserver database using asp.net in encripted form.



First Create a Table into Database having name EncriptFile with the following fields:-

Create Table EncriptFile(Id int primary key identity(1,1), FileName varchar(50), ContentType varchar(50), FileData VarBinary(MAX))


Step 2:- Create a aspx page having a fileUploader control and Button Controll

btnUpload refers the Upload button
Upload refers the FileUpload control

Setp: 3:- Put the Following code on the Click Event of Upload button Controll this code will save the file into database table in encrypted formate. Please Optimize this code according to your Requirement.

protected void btnUpload_Click(object sender, EventArgs e)
{
string FilePath = Upload.PostedFile.FileName;
string FileName = Path.GetFileName(FilePath);
string ext = Path.GetExtension(FilePath);
string contenttype = string.Empty;
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 = Upload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string strQuery = "insert into EncriptFile(FName,ContentType,Data) values(@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@Name", FileName);
cmd.Parameters.AddWithValue("@ContentType", contenttype);
cmd.Parameters.AddWithValue("@Data", bytes);
InsertUpdateData(cmd);
}
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["praveen"].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();
}
}

Setp 4:- using the following code we will display the file on the page by fatching from the database

private void getImage()
{
SqlDataReader red;
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["praveen"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("select * from EncriptFile", con);
cmd.CommandType = CommandType.Text;
con.Open();
red = cmd.ExecuteReader();
if (red.Read())
{
Byte[] bytes =(Byte[]) red["Data"];
// MemoryStream Ms = new MemoryStream();
// Ms.Write()
//Response.ContentType = red["ContentType"].ToString();
//Response.BinaryWrite(bytes);
Response.Clear() ;
Response.ClearHeaders() ;
Response.AddHeader("Content-Disposition", "attachment;filename=praveen.pdf");
Response.AddHeader("Content-Length", bytes.Length.ToString()) ;
Response.ContentType = red["ContentType"].ToString() ;
Response.BinaryWrite(bytes) ;
Response.End();
}
con.Close();
}




No comments:

Post a Comment