Hi Experts,
I have two fields in a table holding the filenames of uploaded Pictures.
foto1 and foto2 (string fields)
Tablename bildata.
Each Photo's filename is renamed on the fly to recordnumberfoto1 and recordnumberfoto2 (ex. 1051foto1.jpg and 1051foto2.jpg)
On the add page (event before record added)
string sql = "SELECT MAX(bilid) as c FROM bildata";
XVar rs = tDAL.CustomQuery(sql);
XVar data = CommonFunctions.db_fetch_array(rs);
int rnoo = data["c"] + 1;
values["glpris"] = values["detailprisimo"];
if (!values["foto1"])
{
}
else
{
var fileArray = MVCFunctions.my_json_decode(values["foto1"]);
for(int i = 0; i < fileArray.Count(); i++)
{
var fileName = fileArray[i]["name"];
var newFileName = String.Format("files/{0}{1}.jpg", Convert.ToString(rnoo).ToLower() + "foto1", i);
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));
fileArray[i]["name"] = newFileName;
}
values["foto1"] = Convert.ToString(rnoo).ToLower()+ "foto10.jpg";
}
if (!values["foto2"])
{
}
else
{
var fileArray = MVCFunctions.my_json_decode(values["foto2"]);
for(int i = 0; i < fileArray.Count(); i++)
{
var fileName = fileArray[i]["name"];
var newFileName = String.Format("files/{0}{1}.jpg", Convert.ToString(rnoo).ToLower() + "foto2", i);
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));
fileArray[i]["name"] = newFileName;
}
values["foto2"] = Convert.ToString(rnoo).ToLower()+ "foto20.jpg";
}
adding records and uploads on addpage Works fine.
On the editpage i have problems!
In before record update event i have:
if (values["foto1"].ToString() != oldvalues["foto1"].ToString()) //means record changed??
{
if (!values["foto1"]) //foto1 is deleted
{
System.IO.File.Delete(MVCFunctions.getabspath(oldvalues["foto1"])); // delete photofile on server to
}
else //foto is changed - new are uploaded
{
var fileArray = MVCFunctions.my_json_decode(values["foto1"]);
for(int i = 0; i < fileArray.Count(); i++)
{
var fileName = fileArray[i]["name"];
var newFileName = String.Format("files/{0}{1}.jpg", XSession.Session["tempbilid"].ToString() + "foto1", i);
System.IO.File.Delete(MVCFunctions.getabspath(newFileName)); // delete old foto i files folder
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));
fileArray[i]["name"] = newFileName;
}
values["foto1"] = XSession.Session["tempbilid"].ToString() + "foto10.jpg"; //save new foto i table field
}
}
if (values["foto2"].ToString() != oldvalues["foto2"].ToString()) //means record changed
{
if (!values["foto2"]) //foto1 is deleted
{
System.IO.File.Delete(MVCFunctions.getabspath(oldvalues["foto2"])); // delete photo on server to
}
else //foto is changed - new are uploaded
{
var fileArray = MVCFunctions.my_json_decode(values["foto2"]);
for(int i = 0; i < fileArray.Count(); i++)
{
var fileName = fileArray[i]["name"];
var newFileName = String.Format("files/{0}{1}.jpg", XSession.Session["tempbilid"].ToString() + "foto2", i);
System.IO.File.Delete(MVCFunctions.getabspath(newFileName)); // delete old foto i files folder
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));
fileArray[i]["name"] = newFileName;
}
values["foto2"] = XSession.Session["tempbilid"].ToString() + "foto20.jpg"; //save new foto i table field
}
}
If no Photos in foto1 and foto2 and i upload foto1 - it Works!
If only foto2 are uploaded the foto1 file are also deleted meaning the if (values["foto1"].ToString() != oldvalues["foto1"].ToString()) return true even if i dont change the content of foto1!!
How to know if only foto2 are changed/deleted/new upoload and not foto1 ??
Best regards and thanks in advance
Michael