ASP Component Building

Tuesday Apr 20th 1999 by ServerWatch Staff
Share:

Write an ASP Com component, what are you nuts?

by Bob Dombroski

Write an ASP Com component, what are you nuts?

Why not? With Visual Basic its pretty easy to tie in the needed ASP object library and access everything you would normally access in your ASP pages. After that its just adding common programming logic to accomplish the task you set out to do. At first it may seem overwhelming, but after putting together a component you will be wanting to create components to do all your common tasks.

So lets come up with something good to write a component for, something we might actually use. How about file uploading thru html? Sounds pretty bland, so lets also set out to do some cool stuff, like allowing for directory changing, multiple files within one upload, returning a collection of files along with some information about the files, a collection of objects and their values from the posting page. Shucks maybe we want to be able to delete a file after we stored it or even move it.

Okay have I got your interest yet? If your saying that this is over your head, read on because you will be surpised how easy it can be.

First a little background on html file transfers. You can find all about the standards set for html by looking up RFC HTML in any search engine. Here is the site I use as a reference http://www.cis.ohio-state.edu/hypertext/information/rfc.html. From there you can read about all the agreed upon standards and the white papers on each RFC standard. It comes in handy when you are coding anything dealing with telnet, ftp, or html.

Unfortunately you can not access the file data with the standard asp request object. You have to get down to the binary information  to get to the data. After that its just parsing out the data from the stream and returning it in a usefull manner. Thats easy!

Sometimes the easiest way to understand a process is to first look at the end result. With this component, if we look at the end result it may be easier to understand the steps in getting there.

The first page is a simple post form with   ENCTYPE="MULTIPART/FORM-DATA". The input type for a file is..file. Here is an example.


<HTML>
<HEAD>
<TITLE>UploadSend file </TITLE></HEAD>
<BODY>
<FORM ACTION="test2.asp"
METHOD="POST" ENCTYPE="multipart/form-data">
<input type = text name = "txt1">Field 1<br>
<INPUT TYPE=FILE NAME="file1"><br>
<input type = text name = "txt2">Field 2<br>
<input type = text name = "txt3">Field 3<br>

<INPUT TYPE="SUBMIT"></FORM>
</BODY></HTML>

Here we show what we want the component to handle and how
the code should work.
<%
Server.ScriptTimeOut = 200  'increase the time out for larger files
Set uploadme = server.CreateObject("bobd.bobdupload") 'create our object

'First we want to have the ability to control the types
'of files being uploaded. We are going to give 2 ways to control
'the file uploads,
bobdadd_illegal_file and bobdadd_legal_file.
'Both will take file types delmited'by commas.
'If
bobdadd_legal_file is set it allows only the those files to be uploaded.
'If
bobdadd_illegal_file is set it files with those ext  will be blocked.
'In a real world situation, only one of these two would be used

uploadme.bobdadd_illegal_file "exe, asp"  ' block  *.exe,  *.asp
uploadme.bobdadd_legal_file "doc, htm" 'allow only  *.doc, *.htm

'We want to give the ability to have default settings for some important
'properties. This allows us to set a standard and not have to set them everytime
'the component is used. It also allows administration to have a control in
'site management. The directory to store and if you are going to overwrite
'existing files will allow defaults thru the following  two methods. These would only
' have to be set once and also could be set by making a registry entry. Any
'Directory setting can be either a physical or a virtual directory
.
uploadme.bobdSetDir = "C:\files"
                 or
uploadme.bobdSetDir = "myweb"
uploadme.bobdSetOverWrite = false

'These two values could also be set on an as needed basis by the following
'2 methods. These 2 override the values in the registry
uploadme.bobdDirectory = "c:\files"
uploadme.bobdOverWrite = TRUE

'We also want to add the ability to have a posting item contain the value
'of the receiving directory, so that we can have user interaction. This property is set
'to the NAME of the input item of the posting form which will contain the directory
'setting.

uploadme.aspRequestDir = "txt2"

'Give the ability to view the raw code being sent. This is great to
'see and understand the data being sent and received.

uploadme.aspDebug = true


'And the method that actually does the file work
uploadme.upload

'The upload method returns 2 collections, the bobdFileNames and
'the bobdRequestParts. The bobdFileNames collections has 2 methods
'and 6 properties. Again it is a collection so iteration is allowed.

For each item in  uploadme.bobdfilenames
     response.write "File Name is " & _
               uploadme.bobdfilenames(item).fName & "<br>"
     response.write "File orig dir is " & _
              uploadme.bobdfilenames(item).fOrigDir & "<br>"
     response.write "File save dir is " & _
              uploadme.bobdfilenames(item).fSaveDir & "<br>"
     response.write "File length is " & _
              uploadme.bobdfilenames(item).fLength & "<br>"
     response.write "File extention is " & _
              uploadme.bobdfilenames(item).fExt & "<br>"
     response.write "File state is " & _
              uploadme.bobdfilenames(item).fstate & "<br>"
Next item
'Most of the properties are self explanatory except for 1
'The file state shows if the  file was stored, the component will not
'cast an error on I/O errors. This is so that  in a multiple file upload,
'the component will store as many files as possible. The asp page
'will need to check to see if the file store was completed. 

'The 2 file methods are remove(to delete the uploaded file) and
'Move(which ,moves(renames) the file. Both return values of true
'if succesful.
if  uploadme.bobdfilenames(item).move("c:\movedir") then
    response.write "The file was moved!"
else
    response.write "File Move failed!"
end if
if  uploadme.bobdfilenames(item).remove  then
    response.write "The file has been deleted"
else
   response.write "File delete failed!"
end if

'bobdRequestParts collection contain what you would normally
'access by Request(item). When doing the file upload we
'have do a binaryread, which makes the request collection
'invalid. The bobdRequestParts replaces the standard
'request collection

my_name = uploadme.asprequestparts("t1")
my_size = uploadme.asprequestparts("t3")


%>
Now that we know what we want the component to do
lets code it

Sometimes the easiest way to understand a process is to first look at the end result. With this component, if we look at the end result it may be easier to understand the steps in getting there.

The first page is a simple post form with   ENCTYPE="MULTIPART/FORM-DATA". The input type for a file is..file. Here is an example.


<HTML>
<HEAD>
<TITLE>UploadSend file </TITLE></HEAD>
<BODY>
<FORM ACTION="test2.asp"
METHOD="POST" ENCTYPE="multipart/form-data">
<input type = text name = "txt1">Field 1<br>
<INPUT TYPE=FILE NAME="file1"><br>
<input type = text name = "txt2">Field 2<br>
<input type = text name = "txt3">Field 3<br>

<INPUT TYPE="SUBMIT"></FORM>
</BODY></HTML>

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved