Persits Software, Inc. Knowledge Base Articles

Upload.Files and Upload.Form collections are empty until a Save method is called

Problem Description

When uploading with AspUpload, if the Upload.Form collection is used before the method Upload.Save ( SaveVirtual , SaveToMemory ) is called, the collection appears empty. As a result, the following code will not work:

<%
' incorrect!
n = Upload.Save(Upload.Form("Path"))
%>

Solution

The collections Upload.Files and Upload.Form are populated by the methods Save, SaveVirtual and SaveToMemory, therefore you must always call one of these methods before the Form and Files collections can be used.

If you want your users to specify a destination path on the same form as the files are selected for uploading, you may use the following two apporaches:

Approach 1: Save to a temp directory, the copy

<%
Upload.Save "c:\temp"
Path = Upload.Form("Path")
For Each File in Upload.Files
  File.Copy Path & "\" & File.ExtractFileName
  File.Delete
Next
%>

Approach 2: Save to memory, then call SaveAs

<%
Upload.SaveToMemory
Path = Upload.Form("Path")
For Each File in Upload.Files
  File.SaveAs Path & "\" & File.ExtractFileName
Next
%>