Aspin.com Programming | Active Server Pages | PseudoRequestDictionary

The PseudoRequestDictionary Script Class

current version: 1.5.00.00 (June 17, 2004)
The PseudoRequestDictionary is a VBScript class that can be used for fileupload. It is created to behave similarly to the standard IIS Request and similarly to several fileupload components that can be found.

advantages:
* It is not needed to install any components.
* It is free code. And you can adapt the code to your desires.
* It is not read-only (IRequestDictionary is read-only). You can add, remove and change key/value pairs whenever you like.

Requirements:
* VBScript version 5 or higher.
* ADODB version 2.5 or higher.

Below you find some sample code for working with the PseudoRequestDictionary.
In the download package you find more information and code samples.
There is a special discussion board created to discuss issues on the PseudoRequest Dictionary Class

download by clicking here.

documentation: list of methods and properties | view history of updates.

 
Basic Coding

How to use the PseudoRequestDictionary for the normal things you might need
How initiate the class, how to read out values in the posted form, and how to handle uploaded files (yes, it can handle multiple uploaded files)
<%
Dim oPseudoRequest, sFormFieldValue1, sFormFieldValue2, element
Dim sFileNameComplete, sFileName, sFileSize, sContentType

Set oPseudoRequest = new PseudoRequestDictionary
oPseudoRequest.ReadRequest()

' reading the complete form will return a URLEncoded string
Response.write oPseudoRequest & "<br>"

' reading out individual form fields
sFormFieldValue1 = oPseudoRequest.Form("formfield1")
sFormFieldValue2 = oPseudoRequest.Form("formfield2")

' looping through the form
For each element in oPseudoRequest.Keys
    Response.write element & "= " & oPseudoRequest.Form(element) & "<br>"
Next

' looping through formfields with multiple values
For each element In oPseudoRequest.Form("formfield").Keys
    Response.write element & "<br>"
Next

' checking for existence of a formfield
If oPseudoRequest.Exists("formfield1") Then Response.write "a field named ""formfield1"" is included in the form<br>"

' alternatively you can check the VarType or the Count property of the formfield
If Vartype(oPseudoRequest.Form("formfield1")) = 8 Then Response.write "a field named ""formfield1"" is included in the form<br>"
If oPseudoRequest.Form("formfield1").Count > 0 Then Response.write "a field named ""formfield1"" is included in the form<br>"

' working with files
If oPseudoRequest.ContainsFile Then Response.write "a file was uploaded<br>"
If oPseudoRequest.Form("formfield1").ContainsFile Then Response.write "field: ""formfield1"" contains a file<br>"

sFileNameComplete = oPseudoRequest.Form("formfield1") ' return filename with complete path
sFileName = oPseudoRequest.Form("formfield1").FileName ' returns filename without path
sFileSize = oPseudoRequest.Form("formfield1").FileSize
sContentType = oPseudoRequest.Form("formfield1").ContentType

oPseudoRequest.Form("formfield1").SaveAs("c:\temp\" & sFileName)
%>
 
Advanced Coding

How to use the PseudoRequestDictionary for special things

For a good understanding, it is important to know that the PseudoRequestDictionary Class stores the values in another class: the PseudoStringList. Just as the class PseudoRequestDictionary tries to resemble the standard IRequestDictionary class, the PseudoStringList tries to resemble the IStringList class.
<%=TypeName(Request.Form)%> and <%=TypeName(Request.Form("formfield1"))%> will show you the names of the IIS classes.
A PseudoRequestDictionary is basically a dictionary object, in which the elements hold a PseudoStringList.
A PseudoStringList is basically an array, holding strings (for normal formfields) or dictionary objects (for file-upload formfields).

The possibility that a PseudoStringList object can hold more elements (even multiple files, or a mix of strings and files), makes it powerfull, but also a bit tricky to use. Some properties and methods are based on the assumption that there is only one file-upload formfield with the same name. But the following html-fields will be captured correctly (even if each file-field will upload a different file):
<form>
<input type="file" name="file1">
<br>
<input type="text" name="file1">
<br>
<input type="file" name="file1">
</form>

There will be a PseudoRequestDictionary.Form("file1") with 3 elements, two of them holding a file, and one holding a string.
See here for a complete list of methods and properties.


thanks to:
Antonin Foller, for providing the ADODB.Recordset Byte2String and String2Byte conversions (http://www.pstruh.cz)
Faisal Khan, for creating the Loader VBScript Class, that inspired me (http://www.stardeveloper.com)