protected void Button2_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string ext = Path.GetExtension(filename);
if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".PNG" || ext == ".JPG" || ext == ".JPEG" || ext == ".gif" || ext == ".GIF")
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;
}
else
{
Response.Write("<script>alert('unsupported format of photo file');</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
}
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;
This code is working well ..
I assume you are trying to display image with base64 string in an System.Web.UI.WebControls.Image
.
first, create a client JavaScript function to set src
attribute of your <img />
tag:
function setImageData(imageBase64) {
document.getElementById("imageId").src = "data:image/png;base64," + imageBase64;
}
then, Invoke that method by
Response.Write("<script>setImageData("+ base64String +")</script>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With