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
set fileArray = CreateDictionary()
set fileArray = my_json_decode(values("fieldname"))
 
' rename each file. In this example - convert to lowercase.
for i = 0 to asp_count(fileArray)-1
   fileArray(i)("usrName") = LCase(fileArray(i)("usrName"))
next
 
' update values of the field that stores file names
values("fieldname") = 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
if bValue(values("fieldname")) then
set fileArray = CreateDictionary()
set fileArray = my_json_decode(values("fieldname"))
Set objFSO = CreateObject("Scripting.FileSystemObject")
' rename files
for i = 0 to asp_count(fileArray)-1
     fileName = fileArray(i)("name")
     newFileName = "files/" & values("LastName") & i & ".jpg"
     sPath = left(getabspath(fileName),len(getabspath(fileName))-len(fileName))
     objFSO.MoveFile  getabspath(fileName) , sPath & newFileName
     fileArray(i)("name") = newFileName
next
end if
 
' update values of the field that stores file names
values("fieldname") = 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