Writing Active Server components in Visual Basic, Page 3
' Class FileIO Option Explicit Private objApplication As Application ' Reference to Application object Private objResponse As Response ' Reference to Response object ' OnStartPage(): ' ' This procedure is called when an instance of this class is created in an ' Active Server Page. It gets references to the Application and Response ' objects that are being used in the ASP page that created the instance of ' this class. ' Public Sub OnStartPage(objScriptingContext As ScriptingContext) Set objApplication = objScriptingContext.Application() Set objResponse = objScriptingContext.Response() End Sub ' OnEndPage(): ' ' This procedure is called when an instance of this class is destroyed in ' an Active Server Page. It is used to clean up object references. ' Public Sub OnEndPage() Set objApplication = Nothing Set objResponse = Nothing End Sub ' help(): ' ' Writes help information to the response stream. ' Public Sub help() objResponse.Write "Class FileIO
" & vbCrLf objResponse.Write "Usage:" & vbCrLf objResponse.Write "set o = Server.CreateObject(""ASCExample.FileIO"") " & vbCrLf objResponse.Write "Methods:" & vbCrLf objResponse.Write "
- " & vbCrLf
objResponse.Write "
- help() - shows help" & vbCrLf objResponse.Write "
- fileSize(strName) - returns size of named file" & vbCrLf objResponse.Write "
- fileExists(strName) - returns True if named file exists" & vbCrLf objResponse.Write "
- deleteFile(strName) - deletes named file" & vbCrLf objResponse.Write "
- appendLine(strName, strLine) - appends line to file" & vbCrLf objResponse.Write "
Testing
Testing your Active Server component is easy (although debugging can get tedious). A few lines of VBScript or JavaScript in an ASP page is all that is required. Here's the test script:
ASCExample Tester
<% ' Create a FileIO object. set objFileIO = Server.CreateObject("ASCExample.FileIO") %><% ' Get some help from the FileIO object. objFileIO.help %>
<% ' Create a file for testing. strLogFileName = "c:\temp\test.log" objFileIO.appendLine strLogFileName, "This is line 1." %> strLogFileName = <%= strLogFileName %>
fileExists() => <%= CStr(objFileIO.fileExists(strLogFileName)) %>
fileSize() => <%= objFileIO.fileSize(strLogFileName) %> bytes <% ' Delete the file. objFileIO.deleteFile(strLogFileName) %>
File deleted.
fileExists() => <%= CStr(objFileIO.fileExists(strLogFileName)) %>
[end]
0 Comments (click to add your comment)
Networking Solutions
