Tutorial 1: Uploading Multiple Files and Using the Form and Files Collections
The objectives of this tutorial are to:
Demonstrate how to save uploaded files and populate the Files and Form collections.
Show how to access the items in the Files and Form collections.
Demonstrate how to use ContentType property to check for the type of files which were uploaded.
- Show how to retrieve the name of the file input boxes responsible for the uploads by using the TagName property.
We are assuming that a form with an EncType of "Multipart/Form-Data" is POSTING data to the ASP page which contains the following code.
Example
<%
'most control methods throw an exception if an error occurs so we will use an
' On Error Resume Next statement for error trapping purposes
On Error Resume Next
'create an instance of the Upload control
Set objUpload = Server.CreateObject ("Dundas.Upload.2")
'we will save all uploaded data (form data as well as any uploaded files) to disk.
'note that we could also save the uploaded files to memory with the SaveToMemory method.
'also note that both the Save and SaveToMemory methods populate the Form and
' Files collections with one method call.
'its also important to realize that by default files saved to disk will have unique filenames, but
' this can be changed with the UseUniqueNames property
objUpload.Save "c:\temp\"
'check to see if method call was successful using VBScript's Err object, if
' an error occurred we will redirect user to a fictitious error page
If Err.Number <> 0 Then
Response.Redirect "Error.asp"
Else
'use a For Each loop and check to see if the uploaded file is an
' executable (utilizing VBScript's InStr method), if it is delete it from disk.
'but first we will output the name of the file input box(es) responsible for uploads
For Each objUploadedFile in objUpload.Files
Response.Write "The "" & objUploadedFile.TagName & "" file input box was used to upload a file.<br>"
If InStr(1,objUploadedFile.ContentType,"octet-stream") Then
'if the default web account does not have the right to delete files
' for the folder you save uploaded files to (in this case c:\temp) then you can either set
' the required rights manually or you could use the ImpersonateUser method
objUploadedFile.Delete
End If
Next
'we will just output the name of the populated form elements and their values
For Each objFormItem In objUpload.Form
Response.Write "<br>The name of the form item is: " & objFormItem
Response.Write "<br>The value of the form item is: " & objFormItem.Value & "<br>"
Next
End If
'Release resources
Set objUpload = Nothing
%>