When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, January 24, 2001

A File-Uploading Windows Script Component, Part 2

By Andy Smith


Read Part 1


In Part 1 we briefly discussed the three objects for our Windows Script Component and looked at the FileUp object in detail. In this part we'll examine the Form and UploadControl objects in the same detail!

- continued -

The Form Object
The major function with the Form object is the Build method. This method is called automatically, so you don't have to worry about it, but it decides if it really needs to parse the Post, and does so. The function that does the parsing is BuildUploadRequest, and is a Private function.

This function, originally written by Philippe Collignon (author of File Uploading with ASP and VBScript), was heavily modified by me to allow for a more object-oriented, robust solution. The internal data representation is still a Dictionary object, though it is now filled with UploadControl objects instead of more Dictionary objects. I won't get into how it parses the Post, that can be better stated in his article.

Probably the only two Properties you'll use in your pages are Item and Items. Item takes either an UploadControl's Name or Index and attempts to return it. Here's how it's implemented:

Function get_Item( varItem )
  Select Case TypeName( varItem )
    Case "Long", "Integer"
      Dim aryKeys
      aryKeys = objRequest.Keys

      Dim intIndex
      intIndex = varItem - 1

      If intIndex >= UBound( aryKeys ) Then
        set get_Item = objRequest.Item( aryKeys( varItem ) )
      Else
        set get_Item = objRequest.Item( varItem )
      End If

    Case Else
      set get_Item = objRequest.Item( varItem )
  End Select

  set get_Item = objRequest.Item( varItem )
End Function

And here's how you access it from ASP:
''Assuming you already created the FileUp object
Response.Write objFileUp.Form.Item("foo")

''Or the short-shortcut
Response.Write objFileUp("foo")

Items is necessary because, as far as anyone I've asked can tell, ( and this includes people with access to the VBScript source code ) it's impossible to implement the For Each ... Next construct within VBScript. So the Items property steals this interface from the Dictionary object that stores the parsed request. You would use it in your code like this:

For Each MyItem in MyRequest.Form.Items
  ''Process MyItem
  Response.Write MyItem
Next

The UploadControl
It has a whole bunch of properties, most of which you'll only use on Files. The default property, Item, returns the value of normal controls, and the filename of File controls. Value returns either a string or the binary data of a file. There is also, FileName, Extension, Path, ContentType, SavePath, and OverWrite for files, as well as Save and SaveAs methods for the File controls. The only ones that are really interesting are the Save and SaveAs methods. They use a FileSystemObject to save the file to the harddisk by Looping through the Binary Data one character at a time, and writing it, like such:

For z = 1 to LenB(UploadControl.Item("Value"))
  objFile.Write chr(AscB(MidB(UploadControl.Item("Value"),z,1)))
Next

Note that if you don't set the SavePath on either this UploadControl or on the FileUp object, it will save the file to the folder that the script is running in.

Some examples of using the UploadControl properties:

Response.Write objFileUp("foo").Value
Response.Write objFileUp("file").FileName
Response.Write objFileUp("file").Path
Response.Write objFileUp("file").ContentType
objFileUp("file").SavePath = "C:\FilesFromFriends\"
objFileUp("file").Save
objFileUp("file").SaveAs( "foo." & objFileUp("file").Extension )

That's all there is to it! The code for the component and an example ASP page is included. Have fun, and happy programming!

  • By Andy Smith


    Attachments:

  • Download the WSC file and example in ZIP format
  • Visit the Upload Article Index
  • Read Building Windows Script Components


  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article