This topic is locked
[SOLVED]

 Rename File Uploaded

5/25/2018 10:18:08 PM
ASPRunner.NET General questions
Akmedlab author

I try this code at After Record Updated, but do not change field values and not move physicaly file to another sub-dir.

// get information about uploaded files

var fileArray = MVCFunctions.my_json_decode(values["IMFotoCard"]);

var cardName = String.Format("Carta
{0}{1}{2}", XSession.Session["UID"].ToString(), values["CD_MeuJogo"].ToString(), values["SQ_NoImg"].ToString());
// rename files

for(int i = 0; i < fileArray.Count(); i++)

{

var fileName = fileArray[i]["name"];

var ext = System.IO.Path.GetExtension(fileName);

var newFileName = String.Format("files/{0}{1}", cardName, i, ext);

System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));

fileArray[i]["name"] = newFileName;

}
// update values of the field that stores file names

values["IM_FotoCard"] = MVCFunctions.my_json_encode(fileArray);

Akmedlab author 5/25/2018

Also, tried this just to get file name with url, but no way..

values["DT_Update"] = new XVar(DateTime.Now);

values["CDUser"] = XSession.Session["UID"];
var cardName = String.Format("Carta
{0}{1}{2}", values["CD_User"].ToString(), oldvalues["CD_MeuJogo"].ToString(), oldvalues["SQ_NoImg"].ToString());

var fileArray = values["IM_FotoCard"];

var ext = System.IO.Path.GetExtension(fileArray["name"]);

var NovaCarta = String.Format("Cartoes/{0}.{1}", fileArray["usrName"], ext);
values["DS_URLEnd"] = HttpContext.Current.Request.Url.AbsoluteUri;

values["DS_URLImg"] = String.Format("{0}{1}{2}{3}", "HTTP://";, HttpContext.Current.Request.Url.Authority, "/" ,NovaCarta);
return true;

Akmedlab author 5/29/2018

I Started a new Project and everything work.

values["DT_Update"] = new XVar(DateTime.Now);

values["CD_User"] = XSession.Session["UID"];

values["DSURLEnd"] = HttpContext.Current.Request.Url.AbsoluteUri;
var ext = "";

var cardName = String.Format("Carta
{0}{1}{2}", XSession.Session["UID"].ToString(), values["CD_MeuJogo"].ToString(), values["SQ_NoImg"].ToString());

var newcardName = String.Format("Cartoes/{0}/" + "{1}", XSession.Session["UserID"].ToString(), cardName);

var thumbcardName = String.Format("Cartoes/{0}/" + "{1}{2}", XSession.Session["UserID"].ToString(), cardName, "thumb");
// get information about uploaded files

var fileArray = MVCFunctions.my_json_decode(values["IM_FotoCard"]);

values["IM_FotoCard"] = MVCFunctions.my_json_encode(fileArray);

// rename files

for(int i = 0; i < fileArray.Count(); i++)

{

var fileName = fileArray[i]["name"];

var themubName = fileArray[i]["thumbnail"];

ext = System.IO.Path.GetExtension(fileArray[i]["name"]);

var newFileName = String.Format("{0}{1}", newcardName, ext);

var newThemubName = String.Format("{0}{1}{2}", newcardName, "thumb", ext);

System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));

System.IO.File.Move(MVCFunctions.getabspath(themubName), MVCFunctions.getabspath(newThemubName));

fileArray[i]["name"] = newFileName;

fileArray[i]["usrName"] = String.Format("{0}{1}", cardName, ext);

fileArray[i]["thumbnail"] = newThemubName;

}

// update values of the field that stores file names

values["IM_FotoCard"] = MVCFunctions.my_json_encode(fileArray);
var newQRName = String.Format("{0}{1}", newcardName, ext);

values["DS_URLImg"] = String.Format("{0}{1}{2}{3}", "HTTP://";, HttpContext.Current.Request.Url.Authority, "/" ,newQRName);