Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
please can anybody solve my problem

Here is my code
i am using file upload control to select an image and btn upload for storing image bytes in session to save later and for preview also ,In this case i am getting Preview correctly.



protected void btnUpload_Click(object sender, EventArgs e)
{

string fileName = PhotoUpload.PostedFile.FileName;
string fileExtension = System.IO.Path.GetExtension(fileName);
string fileMimeType = PhotoUpload.PostedFile.ContentType;
int fileLengthInKB = PhotoUpload.PostedFile.ContentLength / 1024;

string[] matchExtension = { ".jpg", ".png", ".gif" };
string[] matchMimeType = { "image/jpeg", "image/png", "image/gif" };

if (PhotoUpload.HasFile)
{

if (matchExtension.Contains(fileExtension) && matchMimeType.Contains(fileMimeType))
{
if (fileLengthInKB <= 500)
{

Session["ImageBytes"] = PhotoUpload.FileBytes;
byte[] imgData = (byte[])PhotoUpload.FileBytes;
string base64String = Convert.ToBase64String(imgData, 0, imgData.Length);
imgAccper.ImageUrl = "data:image/png;base64," + base64String;
UPimgAccPer.Update();

}
else
{
string display = "Photograph Should be lessthan 500 KB";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);
//Please choose a file less than 1MB
}

}
else
{
//Please choose only jpg, png or gif file.
}
}
else
{
//Please choose a file.
}
}


But after saving i am fetching from database and trying to display it in asp image control bt i am not getting, Please any one check my code and give me solution

Here I am taking all the values from database into datatable called "dtEditRec",and
"UPimgAccPer" is update panel for asp image control named "imgAccper"


Session["ImageBytes"] = (byte[])dtEditRec.Rows[0]["Acc_Person_Photograph"];
byte[] imgData1 = (byte[])dtEditRec.Rows[0]["Acc_Person_Photograph"];
string base64String1 = Convert.ToBase64String(imgData1, 0, imgData1.Length);
imgAccper.ImageUrl = "data:image/png/;base64," + base64String1;
UPimgAccPer.Update();
Posted
Updated 27-Feb-14 5:34am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900