protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetIMage();
}
}
private void GetIMage()
{
string connectionstring = Convert.ToString(ConfigurationManager.ConnectionStrings["ConString"]);
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("Select * from fileUpload", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
img_binary.Visible = true;
byte[] bytes = (byte[])dt.Rows[0]["Filepic"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
img_binary.ImageUrl = "data:image/png;base64," + base64String;
}
}
protected void btnUpload(object sender, EventArgs e)
{
try
{
string connectionstring = Convert.ToString(ConfigurationManager.ConnectionStrings["ConString"]);
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("Insert into fileUpload(FileNames,Filepic,UploadDate) values(@FileNames,@Filepic,@UploadDate)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FileNames", txtfileName.Text.Trim());
cmd.Parameters.AddWithValue("@Filepic", GetBinaryFormat());
cmd.Parameters.AddWithValue("@UploadDate", DateTime.Now);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GetIMage();
}
catch (Exception ex)
{
lblmsg.Text = Convert.ToString(ex);
}
}
private Byte[] GetBinaryFormat()
{
Byte[] bytes = null;
if (uploadData.PostedFile != null)
{
Stream fs = uploadData.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes((Int32)fs.Length);
}
return bytes;
}