This topic is locked
[SOLVED]

 how to validate if new photo is uploaded

2/18/2018 9:48:46 AM
ASPRunner.NET General questions
A
admin author

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

A
admin author 2/19/2018

Further comments to this:
I upload an image to field foto1 and save.

If editing the same record field foto1 ( values["foto1"] ) holds the rigt Picture but oldvalues["foto1"] are empty

If saving the record without changes and editing Again values["foto1"] and oldvalues[foto1"] are the same!!
This is very strange - how come oldvalues["foto1"] empty first time saving??
This explain why my code for validating if the image changes are failling but i don't know how to fix it!!
Kind regards Michael

A
admin author 2/19/2018

SOLVED but strange way!!
I have added two alias fields to querydesigner of table bildata. f01 and f02

Default value of both fields are 0
on edit page I have following in JavaScript onload:
var ctrlfoto1 = Runner.getControl(pageid,'foto1');

var ctrlf01 = Runner.getControl(pageid,'f01');

ctrlfoto1.setValue(ctrlfoto1.getValue());

var ctrlfoto2 = Runner.getControl(pageid,'foto2');

var ctrlf02 = Runner.getControl(pageid,'f02');

ctrlfoto2.setValue(ctrlfoto2.getValue());

function func1() {

ctrlf01.setValue(Number(1));

}

function func2() {

ctrlf02.setValue(Number(1));

}

ctrlfoto1.on('change', func1);

ctrlfoto2.on('change', func2);
The edit page before update now have following;

if (values["f01"] == 1) //means record changed

{

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);

if (System.IO.File.Exists(MVCFunctions.getabspath(newFileName)))

{

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["f02"] == 1) //means record changed

{

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);

if (System.IO.File.Exists(MVCFunctions.getabspath(newFileName)))

{

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

}
How come oldvalues empty but coding my own control Works??
My problem is solved but i am not happy with the solution!!
Best regards Michael