Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Upload

Rename uploaded files

Scroll Prev Next More

 

Each file uploaded to the disk has two names: display name, that you can see on the site, and physical file name on the disk.

 

Let's study how to change both display and physical file names. Use the following code in Add page: Before record added and/or Edit page: Before record updated events.

 

 

1. Change the display name of the uploaded file

 

// get information about uploaded files
var fileArray = MVCFunctions.my_json_decode(values["fieldname"]);
 
// rename each file. In this example - convert to lowercase.
for(int i = 0; i < fileArray.Count(); i++)
{
fileArray[i]["usrName"] = MVCFunctions.strtolower(fileArray[i]["usrName"]);
}
 
// update values of the field that stores file names
values["fieldname"] = MVCFunctions.my_json_encode(fileArray);

 

Note: In this example, fieldname stores the names of uploaded files.

 

2. Change the physical name of the uploaded file

 

This code snippet changes the file names to the LastName field values.

 

// get information about uploaded files
var fileArray = MVCFunctions.my_json_decode(values["fieldname"]);
 
// rename files
for(int i = 0; i < fileArray.Count(); i++)
{
var fileName = fileArray[i]["name"];
var newFileName = String.Format("files/{0}{1}.jpg", values["LastName"], i);
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));
fileArray[i]["name"] = newFileName;
}
 
// update values of the field that stores file names
values["fieldname"] = MVCFunctions.my_json_encode(fileArray);

 

Note: In this example, fieldname stores the names of uploaded files.

See also:

Export/Import pages

"View as" settings: File

"Edit as" settings: File/Image