Tutorial 2: Retrieving Form Data Incrementally Using the GetNextFile Method
The objectives of this tutorial are to:
Demonstrate how to optionally upload files using the GetNextFile method (retrieves form data incrementally).
Show how the Form collection is populated by GetNextFile method calls.
Demonstrate how to use the TagName property to check which uploaded files originated from which file input boxes.
- Demonstrate how to use the ContentType property to determine the type of file the user wishes to upload.
We are assuming that a form with an EncType of "Multipart/Form-Data" is POSTING data to an 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")
'retrieve the first NextFile object, which contains header data only for
' the first file uploaded by user. Note that the Form collection will be
' populated with all form data which occurs up to the first populated
' file input box (which results in this first NextFile object)
Set objNextFile = objUpload.GetNextFile
'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"
'we will retrieve NextFile objects until there are no more uploaded files
' to process (each NextFile object corresponds to a populated file input box)
'if the file is not an executable we will save the uploaded file to memory, if
' it is an executable then we will not save it at all (by just calling GetNextFile again)
Do Until objNextFile Is Nothing
'NOTE: you can retrieve any form data here as long as it occurs in the html POST form
' BEFORE the populated file input box which corresponds to the current NextFile object
strSomestring = objUpload.Form("SomeFormElement")
'now save file if not an *.exe, and for demonstration purposes we will output
' the name of the file input box from which the uploaded file originated
If InStr(1,objNextFile.ContentType,"octet-stream",1) = 0 Then
objNextFile.SaveToMemory
Response.Write objNextFile.TagName & "<br>"
End If
'call NextFile again, to retrieve the header data for the next uploaded file
' once again it should be noted that this will populate the Form collection
' with all data up to the corresponding populated file input box
Set objNextFile = objUpload.GetNextFile
Loop
'release resources
Set objUpload = Nothing
%>