Thursday, February 4, 2010

HOW TO SAVE USERS IMAGE IN DB: STORE ONLY PATH IN DB AND IMAGES IN FOLDER ON SERVER

protected void btnupload_Click(object sender, EventArgs e)
{
try
{
BLL_Users objimage = new BLL_Users();
objimage.UserID = Convert.ToInt16(Session["UserID"].ToString());
string filename = ImageUpload.FileName;//find the extension of the uploaded image.convert it into lower case.


//if following 2 lines(which remove readonly pro of the image) r make enable then images from client m/c will not

save to server
//System.IO.FileInfo fileInfo = new System.IO.FileInfo(ImageUpload.PostedFile.FileName);//first remove readonly

of any image from wher r u uploding
//fileInfo.IsReadOnly = false;

string fileName = Server.HtmlEncode(FileUpload1.FileName);
string ext= System.IO.Path.GetExtension(fileName);

if (ImageUpload.HasFile && ext == ".jpeg" || ext == ".jpg" || ext == ".bmp" || ext == ".png" || ext == ".gif")
{
ImagePath = Server.MapPath("../"); // find LMSBeta
string ImagePath1 = ImagePath + "UserImages"; // attach /LMSBeta/UserImages
objimage.ImageURL = Session["FirstName"].ToString() + Session["LastName"].ToString() + ext; //send

username+ext
objimage.UpdateUserImage();

ImageUpload.SaveAs(ImagePath1 + "\\" + Session["FirstName"] + Session["LastName"]+ ext); //if user register

then only save his image to disk with his username + ext.
Image1.ImageUrl = ImagePath1 + "\\" + Session["FirstName"] + Session["LastName"] + ext;
Response.Redirect("ViewProfile.aspx?action=viewprofile",false);
}
else
{
Session["imgErr"] = "Only .jpeg, .jpg, .bmp, .png, .gif file formats supported.";
Response.Redirect("ViewProfile.aspx?action=viewprofile",false);
}
}
catch(Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
LCE.Configuration.ErrorLog("Include/UserRegistration.cs", errorMessage);
}
}

No comments:

Post a Comment