Hello,
I want to display binary image from database on image control using entity frame work in asp.net c# but it wont work please help me out from this problem. I have tried below code:
.aspx.cs code
protected void rbUpload_Click(object sender, EventArgs e)
{
EmployeePortal.Models.NCI.NCIEntities ent = EmployeePortal.Models.ClsNCIUtil.GetNCIEntity(Session["CustomerID"].ToString());
string folderPath = Server.MapPath("~/Forms/Uploads/");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
Image1.ImageUrl = "~/Forms/Uploads/" + Path.GetFileName(FileUpload1.FileName);
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
EmployeeImage file = new EmployeeImage
{
EmployeeId=empno,
EmpImage = bytes
};
ent.EmployeeImages.Add(file);
ent.SaveChanges();
}
protected void loadimg()
{
EmployeePortal.Models.NCI.NCIEntities ent = EmployeePortal.Models.ClsNCIUtil.GetNCIEntity(Session["CustomerID"].ToString());
using (ent)
{
}
}
.aspx code
<div style="float:none">
<table>
<tr>
<td style="text-align:left">
<asp:Label ID="lblAllow" runat="server" Text="Allowed file types: TIF, PNG, JPG" ></asp:Label>
<div class="demo-container size-wide">
<asp:FileUpload ID="FileUpload1" runat="server" TargetFolder="Forms\\Uploads" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.jpg|.JPG|.png|.PNG|.tif|.TIF)$"
ControlToValidate="FileUpload1" runat="server" ForeColor="Red" ErrorMessage="Only JPG, PNG and TIF files are allowded."
Display="Dynamic" />
<hr />
<div style="text-align:center">
<Telerik:RadButton ID="rbUpload" runat="server" Text="Upload Selected" skin="Windows7" style="align-self:center" OnClick="rbUpload_Click">
<Icon PrimaryIconCssClass="rbUpload" />
</Telerik:RadButton>
</div>
</div>
</td>
</tr>
</table>
</div>
Please help me as soon as possible and i am storing image in binary format..
Download FREE API for Word, Excel and PDF in ASP.Net:
Download
Hi JennyD6856,
Please refer below sample.
HTML
<asp:Image ID="imgDisplay" runat="server" />
Namespaces
C#
using System.IO;
using System.Linq;
using dbFilesModel;
VB.Net
Imports dbFilesModel
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
using (dbFilesEntities entities = new dbFilesEntities())
{
byte[] bytes = (from file in entities.tblFiles
select file.Data).FirstOrDefault();
string base64 = Convert.ToBase64String(bytes);
imgDisplay.ImageUrl = string.Format("data:image/gif;base64," + base64 + ");");
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Using entities As dbFilesEntities = New dbFilesEntities()
Dim bytes As Byte() = (From file In entities.tblFiles Select file.Data).FirstOrDefault()
Dim base64 As String = Convert.ToBase64String(bytes)
imgDisplay.ImageUrl = String.Format("data:image/gif;base64," & base64 & ");")
End Using
End Sub
Screenshot
