Uploading Files to a server

With ActiveFile you can upload files from any web browser or client that supports the RFC 1867 protocol. The topics that follow show how easy it is to add this capability to your Active Server Pages application.

Upload Topics

Defining your HTML form for file upload
Processing the file upload on the server
Uploading multiple files
Handling both file and non-file form data

 

   Defining your HTML form for file upload

The user interface for uploading files from a web browser is defined as an HTML form. Whether you are modifying an existing HTML form or creating a new one from scratch, here are some easy steps to follow so that you can upload files from your form:

  1. The HTML <FORM> element needs to include attribute enctype="multipart/form-data" and attribute 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. The HTML <INPUT> element for each file you want to upload needs to include 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.

Here is a sample HTML form for uploading a file to the server:

 
<form action="upload.asp" method="POST" enctype="multipart/form-data">
  <strong>Select file:</strong> 
  <input type="file" size="20" name="UploadedFile">
  <br><input type="submit">
</form>

 

   Processing the file upload on the server

The action attribute of your HTML <FORM> tag must point to the Active Server Page that uses ActiveFile to process the data. This Active Server Page should follow these steps in order to process the uploaded data:

  1. Create a Post object:

    Set Post = Server.CreateObject("ActiveFile.Post")
     
  2. Call Post.Upload to process the upload request

    Post.Upload "C:\UPLOAD"

    If the files you are uploading will only be on the system temporarily, you may want to create a temporary directory as follows:
     
    Set Directory = Server.CreateObject("ActiveFile.Directory")
    Directory.CreateTemp "C:\UPLOAD"
    Post.Upload Directory.Path
     
  3. 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("UploadedFile").File.Remove

Here is a complete example that you can use with your Active Server Page:

 
' Perform the upload
Set Post = Server.CreateObject("ActiveFile.Post")
Post.Upload "C:\TEMP"

' Display the results
For Each FormInput In Post.FormInputs
    Response.Write FormInput.Name & " = " & FormInput.Value & "<BR>"
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. Also note that this example displays the client-side source file pathname stored in FormInput.Value. The target location of the file on the server can be found in FormInput.File.Name.

For a more complete example, take a look at the ActiveFile Explorer sample application that demonstrates both file upload and download.

 

 

 

  Uploading multiple files

ActiveFile can handle uploading multiple files in a single request. However, a separate <INPUT TYPE="file"> form element must be added to your HTML form for each file that needs to be uploaded. For example, here is an HTML form for uploading two files to the server:

 
<form action="upload.asp" method="POST" enctype="multipart/form-data">
  <strong>Select file:</strong> 
  <input type="file" size="20" name="UploadedFile1">
  <input type="file" size="20" name="UploadedFile2">
  <br><input type="submit">
</form>

Note that the <INPUT TYPE="file"> elements have been given different names, UploadedFile1 and UploadedFile2. These names can then be used in the upload.asp script to index the Post.FormInputs collection and reference the files individually.

! Each <INPUT TYPE="file"> element in your HTML form must have a unique name in order for multiple files to be uploaded properly.

An alternative to using separate <INPUT TYPE="file"> form elements for uploading files is the AppletFile Upload Applet. This Java applet can be used in place of <INPUT TYPE="file"> form elements and is capable of uploading an arbitrary number of files. For more information, visit the AppletFile home page at http://www.infomentum.com/appletfile.  

 

   Handling both file and non-file form data

In addition to uploading files, ActiveFile can also process non-file data from other types of form elements that are on the same HTML form. You can mix and match both <INPUT TYPE="file"> form elements and other types of form elements on your HTML form, and all of this data will be available in the Post.FormInputs collection after you call Post.Upload.

!
Be sure to call Post.Upload before referencing any data in the FormInputs collection.  Otherwise, your files may not be uploaded with the correct file names. See Post.Upload for more information.

If you are modifying an existing application to use ActiveFile, you will need to change all references to form data to use the Post.FormInputs collection instead of Active Server Page's Request object.

! Once you have modified your HTML form for file uploading, you must never use Active Server Page's built-in Request object to access the data posted by this form. Because the Request object does not recognize RFC 1867 request, the data will be corrupted if your script references either the Request collection or the Request.Form collection.
File Upload File Download File Upload File Download File Upload File Download File Upload File Download File Upload File Download File Upload File Download ASP .NET Drag n Drop Java Mac OS X