Show/Hide Toolbars

Navigation: Advanced topics > Events > Sample events > Upload

asprunnernet32x32     ASPRunner.NET manual


Rename uploaded files

Scroll Prev Next More

 

Each uploaded to the disk file has two names: display name, that you can see on the site, and physical file name on the disk. Let's study how to rename both display and physical file names. Use the following code in Add page: Before record added and/or Edit page: Before record updated events.

 

yellowbulbNote: Change the values listed in red to match your specific needs.

 

1. Rename display name of uploaded file

 

C#

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

 

In this example fieldname stores names of uploaded files.

 

2. Rename physical name of uploaded file

 

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

 

C#

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

 

In this example fieldname stores names of uploaded files.