ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
An Overview of ASP.NET
Connections, Commands, And Procedures
What is ASP?

QUICK TIP:
Use VBScript's TypeName to resolve type mismatch errors
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


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:

  1. 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.

  2. 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.

Form-based Uploading

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:

  1. A <FORM> tag with attributes enctype="multipart/form-data" and method="POST".   For example:

    <form action="upload.asp" method="POST" enctype="multipart/form-data">

    where upload.asp is an Active Server Page containing the necessary ActiveFile code to process the file upload.
     
  2. An <INPUT> tag for each file you want to upload with attribute type="file".  For example:

    <input type="file" size="20" name="UploadedFile">

    where UploadedFile is the name of the input box for processing by the server.

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:

 
<body bgcolor="#c0c0c0">
<form action="upload.asp" method="POST" enctype="multipart/form-data">
  <strong>Select file #1:</strong><br> 
  <input type="file" size="40" name="FILE1"><p>
  <strong>Select file #2:</strong><br> 
  <input type="file" size="40" name="FILE2"><p>
  <strong>Comments:</strong><br> 
  <input type="text" size="40" name="COMMENT"><p>
  <input type="submit" value="Upload!">
</form>

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:

 
<body bgcolor="#c0c0c0">
<strong>Select files for upload:</strong><br>
<applet code="FileUpload.class" archive="FileUpload.jar"
        codebase="/AppletFile/classes" width="420" height="64" 
        name="FileUpload" mayscript>
<param name="cabbase" value="/AppletFile/classes/FileUpload.cab">
</applet>
<form action="upload.asp" 
 onsubmit="return document.FileUpload.submit(document,this,true)">
  <strong>Comments:</strong><br> 
  <input type="text" size="46" name="COMMENT"><p>
  <input type="submit" value="Upload!">
</form>

Processing the File Upload on the Server

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:

  1. Create a Post object:

    Set Post = Server.CreateObject("ActiveFile.Post")
     
  2. 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.

For More Information

The complete text of this article and sample scripts can be downloaded from:

http://www.infomentum.com/whitepapers/uploading.zip

To learn more about uploading files with ActiveFile and AppletFile, I invite you to visit the following resources on our web site.

ActiveFile home page: http://www.infomentum.com/activefile/
AppletFile home page: http://www.infomentum.com/appletfile/
Free downloads: http://www.infomentum.com/download/

1997-1999  Infomentum Ltd.  All rights reserved.

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers