Show/Hide Toolbars

Navigation: Advanced topics > Events > Sample events > Upload

ASPrunnerPro32x32     ASPRunnerPro 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

 

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

 

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.

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

 

In this example fieldname stores names of uploaded files.