Published: Wednesday, January 24, 2001
A File-Uploading Windows Script Component
By Andy Smith
Introduction
Probably one of the most asked question on the ASP Messageboard
is how to upload files from a Web form. Usually, the answer is simple: "Buy or Download a component."
Unfortunately, not everybody can use 3rd party components on their server, especially those
in hosted environments. I was once one of those people. Additionally, those components are oftentimes
not very affordable for someone who works with ASP as a hobby. (For a listing of third-party
upload components, be sure to check out the Uploading
Article Index!)
After having promised a client that I could give them a Web-based file upload, and then
finding out I couldn't register a component on the server, I started sweating, imagining
many future late nights at work trying to solve this horrible issue.
Then I found a wonderful article on ASPToday.com -
Uploading with ASP and VBScript -
that solved the problem with a couple functions. Voila! After using it for that client, I
came to realize that, while the solution provided there was functional, it wasn't very user
friendly. About the same time, I started getting interested in the usefulness of Windows
Script Components as a replacement for the inability to register compiled components.
(If you are unfamiliar with Windows Script Components be sure to read:
Building Windows Script Components!)
Naturally, I decided to whip myself up a nicely object oriented, encapsulated and compatible
version of the File Upload function I had been using.
The Gist Of It
The whole thing breaks down into three objects, all packaged together into one file.
The FileUp object
This serves as a compatibility layer between my object and the normal ASP Request object.
It has properties and methods that span my Form Request object as well as the other
intrinsic Request objects. It also has routines for setting defaults for the object.
It's default property is Item, which gives compatibility with the ASP Request
object.
The Form object
This one does all the real magic of parsing the Post and giving access to the items. This
object essentially takes the place of the Form object in the normal Request. The default
property for it is Item, which gives compatibility with the ASP Request.Form
object
The UploadControl object
Each item is stored in one of these, and is accessed by the Form object's default Item
property. The default Value property returns the value of normal form fields, and the
filename of file fields.
Using the object is almost exactly the same as using the normal ASP request object. Here's
some sample code that you would use in your page to test if the page had been submitted by
testing the value of the submit control, and then writing out a couple of the
values as well as saving the file to the current folder.
Dim MBRequest
Set MBRequest = GetObject( "script:" & _
Server.MapPath( "MetaBuilders.FileUp.wsc" ) )
If MBRequest("submit") = "upload" then
Response.Write MBRequest("MyTextField") & vbNewLine
Response.Write MBRequest("File1").Path & _
MBRequest("File1").FileName & vbNewLine
MBRequest("File1").Save
End If
|
The FileUp Object
When you create the
FileUp object, a few things happen. First, it checks to see
if it should do anything at all with the line:
If InStr( 1, Request.ServerVariables("HTTP_CONTENT_TYPE"), _
"multipart", vbTextCompare ) > 0 Then
|
This checks to make sure that the form that posted to the page used the encoding type that
is need to send files at all. If it wasn't used, then all requests made to the FileUp
object will be directly forwarded to the ASP Request object.
Assuming that the proper enctype is found, then a new Form object is
created and is told to parse and populate itself with these lines:
set objForm = CreateComponent("Form")
objForm.Build
objForm.SetOverWriteFiles( blnpOverWrite )
objForm.SetSavePath( strpSavePath )
|
The CreateComponent function is a VBScript function used for WSC's to reference
other Components in the WSC Package. Build, tells the object to parse the Post,
and the last 2 set default behavior for File Saving operations.
Next a dummy, empty Form object is also created to allow for people trying to
access nonexistent items in the request in the same way they can use the ASP Request object.
This object has only one UploadControl in it, named "empty" and that is what is
always returned when no item can be found that matches what the page asks for. It's created
exactly the same as the real object, except that the line:
objEmpty.BuildEmpty = True
|
is set before it's Build method is called.
The default Item property gives the compatibility with ASP Request. It allows the
client to reference items with the Request("itemName") syntax, and it still
searches the Request in the same order as ASP would. It does this with code like:
Set strValue = ThisASP.Request.QueryString( strKey )
If strValue <> "" Then
Set get_Item = strValue
Exit Function
End If
|
You can also use the Request.Form("itemName") syntax thanks to the Form
property. It searches either itself, or the intrinsic Form object, depending on the enctype
of the Post, which was found earlier.
Two new properties are OverWrite and SavePath. These two do what
you probably think they do. They tell each UploadControl what to do when
.Save or .SaveAs is called on a file object. Setting OverWrite
to true allows files to be overwritten without an error. SavePath, which defaults
to the folder which the script is running in, tells the UploadControls where to
save files to. They do this by calling the Form object's SetOverWriteFiles
and SetSavePath methods.
This concludes our examination of the FileUp object. In
Part 2 we'll take a close look at the Form
and UploadControl objects!
Read Part 2!