Uploading Multiple Files to your
ASP script
By Bill Raudabaugh, Infomentum
A Brief History
Sending files to web browsers has always been easy. However, uploading files to the web
server has been a very difficult task. Your options were pretty much limited to using FTP,
FrontPage, or some other site management tool. These options have two fundamental
problems:
Security Risk. They require NT accounts to be
assigned or shared with anyone using these tools. And once inside, access to files can
only be controlled through tedious management of file permissions.
Not Scriptable. Neither of these tools can be
integrated into your ASP application.
In 1997, Infomentum ran head on into this problem while developing custom ASP
applications for its clients. After an exhaustive search for a solution, we found that
initial attempts by Microsoft and other vendors to solve this problem came up short in
several key areas. They either failed to be truly integrated with ASP or couldn't handle
uploading multiple files and form data at the same time. Our conclusion was that if we
wanted a real solution, we were going to have to build it ourselves. That decision
resulted in our ActiveFile
product, which shipped in November of 1997 and is currently in use at thousands of sites
worldwide.
To begin using ActiveFile to upload files, you first need to create an HTML form that
will allow files to be selected. At a minimum, your HTML form must include:
A <FORM> tag with attributes enctype="multipart/form-data" and
method="POST". For example:
You can add multiple <INPUT> tags for both file and non-file data. Here is
a simple HTML form for uploading multiple files and a comment to the server:
If you need to upload an arbitrarily large number of files, it may not be practical to
use individual <INPUT TYPE="file"> tags for each file. Face it, if your
user has to use a different 'Browse...' button to select each file, they are not going to
be happy if they need to upload more than one or two files. To solve this problem,
Infomentum developed the AppletFile
Upload Applet, a Java Applet that can be used in place of <INPUT
TYPE="file"> tags. To use AppletFile, simply remove the <INPUT
TYPE="file"> tags and replace them with AppletFile so that the HTML form
looks like:
The action attribute of your <FORM> tag must point to an ASP script that will
process the upload. Using ActiveFile, your ASP script only needs these two simple lines of
code:
Create a Post object:
Set Post = Server.CreateObject("ActiveFile.Post")
Call Post.Upload to process the upload request
Post.Upload "C:\UPLOAD"
After the call to Post.Upload, your application can reference the
Post.FormInputs().File object to perform application-specific processing on the files. For
example, the following line of code can be used to delete the uploaded file if you no
longer need it.
Post.FormInputs("FILE1").File.Delete
Here is a complete example that performs the upload and displays a summary of all the
information collected:
<%
' Perform the upload
Set Post = Server.CreateObject("ActiveFile.Post")
Post.Upload "C:\TEMP"
' Display a summary of the uploaded data
For Each FormInput In Post.FormInputs
If FormInput.ContentType <> "" Then
' Display uploaded file information
Response.Write FormInput.Name & " = " & FormInput.File.FileName & _
", size=" & FormInput.File.Size & "<BR>"
Else
' Display form element name and value
Response.Write FormInput.Name & " = " & FormInput.Value & "<BR>"
End If
Next
%>
In the above example, ActiveFile will upload the files into the C:\TEMP directory using
the original client file names along with versioning to prevent any existing files from
being overwritten. Other upload options are available. This example also iterates over the
Post.FormInputs collection to access the data. By checking to see if the ContentType
property is set, it is possible for the script to identify uploaded files without
hardwiring element names.