7.1.6- Properties and Methods of the Request Object
Created
by Brendan Doss.
|
| << 7.1.5- Request Object Collection Shorthand | Chapter7 | 7.1.7- The Response Object >> |
Properties and Methods of the Request Object
The Request object also has a single property and a single method: we'll look at these briefly now. Their uses are interlinked and while being quite arcane, there might just be times when they're invaluable. If you're uploading a file or image from your browser to a web server, then they might just be what you need.
The TotalBytes Property
The TotalBytes property holds the total number of bytes the client sent in the body of the HTTP Request:
This Request's size is : <%= Request.TotalBytes%> Bytes.
You might be tempted to say, so what, when am I going to use such a property? Well then, read on.
The BinaryRead Method
The TotalBytes property's real use becomes apparent when used with the BinaryRead method. The BinaryRead method retrieves data sent to the server from the client as part of a POST request sent by a form and stores it in a Safe Array of bytes. (A Safe Array is an array that contains information about the number of dimensions and the bounds of its dimensions.) This would be useful when the information contained within the form is something other than text.
Unlike the Form or QueryString collections, this method allows you to take the raw data from the form and parcel it up in a safe array of bytes. The problem is a safe array of bytes isn't exactly the most useful or user-friendly format and if you try to display the information directly using Response.Write, you will generate an error. If you wish to use the whole block then you need to know when the whole block of data has been transmitted in its entirety, and this is where the TotalBytes property comes in. The TotalBytes property is passed to the BinaryRead method as a parameter to indicate when to finish reading the data it has received, as follows:
<%
Dim bread, bytecnt
bytecnt = Request.TotalBytes
bread = Request.BinaryRead(bytecnt)
You can then use a special VBScript function, MidB to 'unwrap' the information from the individual bytes of the array.
For i = 1 to bytecnt
Response.Write MidB( bread, i, 1 )
Next
%>
Then the information from the form can be used. The point is that we cannot mix and match binary read with standard form manipulation. So bearing this in mind, let's take a look at a quick example. In this example, we'll pass a first name, last name and password over and use the binary method to unwrap it. This is pretty much what we've already done with the Querystring and Form collections, and if you were using plain text, then you'd use one of these two collections ahead of this method. However when transferring something other than pure text (zipped file for example?), then the following method will come in useful.
Try It Out – Using the BinaryRead Method and TotalBytes Property
1. Open your HTML editor, create a new file, and type in the following:
<HTML>
<HEAD>
<TITLE>Request BinaryRead</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<FORM NAME=BinaryRead ACTION="BinaryResponse.asp" METHOD="POST">
Type your first name: <INPUT TYPE="TEXT" NAME="firstname">
<BR>
Type your last name: <INPUT TYPE="TEXT" NAME="lastname">
<BR>
Type your password: <INPUT TYPE="TEXT" NAME="password">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Login">
</FORM>
</BODY>
</HTML>
2. Save it as BinaryForm.asp and close it and the type in the following:
<HTML>
<BODY>
<%
intByteCnt = Request.TotalBytes
intBread = Request.BinaryRead(intByteCnt)
For intLoop = 1 to intByteCnt
Response.Write MidB( intBread, intLoop, 1 )
Next
%>
</BODY>
</HTML>
3. Save this as BinaryResponse.asp and open up BinaryForm.asp in your browser.
|
4. Click on Login to submit the contents:
|
How It Works
Ok, we could have returned the contents of the Request.Form collection in this example and saved us all some code. The point however is to demonstrate that if you wished to use the form to upload some other file format apart from raw text, such as a graphics file, you could use BinaryRead to unload it at the other end.
In BinaryForm.asp we used the POST method to send the Form:
<FORM NAME=BinaryRead ACTION="BinaryResponse.asp" METHOD="POST">
However, when we got back the information we needed slightly more code to unwrap it. We started determining the total number of bytes in the file and assigned it to the variable intByteCnt:
intByteCnt = Request.TotalBytes
We then used the BinaryRead method, passing the Total Bytes count as a parameter:
intBread = Request.BinaryRead(intByteCnt)
Then we were able to loop through our data, one byte at a time, using the MidB function described previously. In this way, we were actually displaying one character at a time on the screen:
For intLoop = 1 to intByteCnt
Response.Write MidB( intBread, intLoop, 1 )
Next
An important thing to note is that once you have called BinaryRead, you cannot query the other Request collections directly and vice versa, you cannot call binary read after calling the Request collection – the data is effectively transformed to a binary format.
| << 7.1.5- Request Object Collection Shorthand | Chapter7 | 7.1.7- The Response Object >> |

RSS


