Search Persits Software Knowledge Base

 

Article PS02041192

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.

Created: 4/11/2002 12:50:23 PM
Last Modified: 12/15/2010 3:26:39 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.