PDA

View Full Version : سوال: عدم نمایش عکس صحیح در کنترل Image



teardrop
یک شنبه 25 فروردین 1392, 21:57 عصر
به نام خدا

سلام

برای پروژه ام نیاز به ارسال عکس یا همون آپلود عکس دارم، عکس رو تونستم آپلود و ذخیره کنم. ولی وقتی که می خوام عکس رو از دایرکتوری مربوطه نمایش بدم با مشکل مواجه می شم. یعنی وقتی که عکس تو سایت نمایش داده می شه با دایرکتوری فرق می کنه یعنی اصلا اون عکس وجود نداره. بار اولی که عکس رو آپلود می کنم مشکلی نداره ولی بار دوم که آپلود می کنم و ذخیره میشه تو سایت عکس قبلی رو نشون میده در حالی که اون عکس با عکس جدید در دایرکتوری مربوطه رونویسی شده.
کد ذخیره عکس:


/// <summary>
/// Save image from uploader
/// </summary>
/// <param name="name">name of image</param>
private void SaveImage(string name)
{
try
{
if (Session["fileContentsImgEmployee"] != null)
{
string nameOfImg = name.ToString() + ".jpg";
string savePath = Server.MapPath("~/Images/EmployeeImages/");
byte[] bufferImgEmployee = (byte[])Session["fileContentsImgEmployee"];
// Save to directory of images
System.IO.File.WriteAllBytes(savePath + nameOfImg, bufferImgEmployee);

// Remove session
Session.Remove("fileContentsImgEmployee");
}
}
catch (Exception ex)
{
LabelError.Text = ex.Message;
}
}


کد نمایش عکس :


if (System.IO.File.Exists(Server.MapPath("~/Images/EmployeeImages/") + query.p_ + ".jpg"))
{
employeeImage.Width = 80;
employeeImage.Height = 80;
// It is important when the image is not showed, upper and lower words must checked by explorer(firefox, ie) with generated address asp.net
employeeImage.ImageUrl = "~/images/EmployeeImages/" + query.p_ + ".jpg";
}
else
employeeImage.ImageUrl = "";




102838102839

teardrop
یک شنبه 25 فروردین 1392, 22:31 عصر
فکر کنم خودم جواب خودم رو پیدا کردم اگه باز به مشکل نخوره:



Hi i have a problem with my image control that is when i upload an image it shows that image but when i update it, its shows old image while updates in data base i think it gets image from cache is there any technique to handle this issue kindly tell me.
Image1.ImageUrl = populatedata.ResultSet.Tables[0].Rows[0][12].ToString(); string file_ext = Path.GetFileName(Image1.ImageUrl); Image1.ImageUrl = "~/EmpImages/" + file_ext;




You can change the image url everytime you get it through some random generated query string.
This way the browser won't cache it.
For instance:
int randomNumber = (new Random()).Next(0, 10000); Image1.ImageUrl = "~/EmpImages/" + file_ext + "?rand=" + randomNumber; But if the problem only happens sporadically and image updates aren't so oftem, you can just press Ctrl+F5 on the browser and it will refresh all files.




http://stackoverflow.com/a/5933907/1649834