Persits Software, Inc. Knowledge Base Articles

HOWTO: Renaming uploaded files

Problem Description

By default, AspUpload always saves uploaded files under their original names. This article describes two methods to rename the files.

Solution

The following code samples rename a single uploaded file using the current session ID. You can use your own naming scheme instead.

Method 1: Save to disk, then copy

<%
Set Upload = Server.CreateObject("Persits.Upload")

' Prevent overwriting
Upload.OverwriteFiles = False

' Save to disk
Upload.Save "c:\upload"

' Use session ID as the new file name
NewName = Session.SessionID

For Each File in Upload.Files
   File.Copy "c:\upload\" & NewName & File.ext
   File.Delete
   Response.Write "New name: " & NewName & File.ext & "<BR>"
Next
%>

Method 2: Save to memory, then call SaveAs

<%
Set Upload = Server.CreateObject("Persits.Upload")

' Prevent overwriting
Upload.OverwriteFiles = False

' We use memory uploads, so limit file size
Upload.SetMaxSize 1000000, true

' Save to memory
Upload.Save

' Use session ID as the new file name
NewName = Session.SessionID

For Each File in Upload.Files
   File.SaveAs "c:\upload\" & NewName & File.ext
   Response.Write "New name: " & File.FileName & "<BR>"
Next
%>

Note that these code samples are based on AspUpload 3.0+. Older versions of AspUpload can be used as well, but certain changes would have to be made to the script.