Published: Tuesday, July 18, 2000
Speed up your ASP pages: Turn Them into HTML with ASPTear!, Part 2
By Evagoras Charalambous
Read Part 1
In Part 1 we described the goal of this article, to cache the output of
an ASP page in an HTML file. We also discussed how to use ASPTear, a free commponent, to grab the output of
an ASP page. In this part we'll look at saving this output to a text file.
Part 2: Writing to the HTML file
Keep in mind where we are at this point. We are in the processing page, the page that does all the database
updates and so on. So, now we have a string with the HTML page returned to us. We need to write this to a
plain HTML file, and if one is already there simply replace it. We use the FileSystemObject to
accomplish this (To learn more about the FileSystemObject be sure to take a moment and
read the FileSystemObject F.A.Q..):
Sub CreateHTMLPage(getURL,postFile)
'Continued from Part 1 of this article...
...
'write to file...
Dim objFile, objTStream
Set objFile = Server.CreateObject("Scripting.FileSystemObject")
Set objTStream = objFile.OpenTextFile(postPage, 2, True, 0)
objTStream.Write(strRetrieval)
'Clean up...
Set objTStream = Nothing
Set objFile = Nothing
End Sub
...
|
As you might recall the OpenTextFile method takes 4 parameters:
| Parameter | Meaning |
postPage | Relative or absolute path of the file you are writing/reading (absolute is usually faster) |
2 | Determines the I/O mode: 1 ForReading, 2 ForWriting, and 8 ForAppending |
True | A new file will be created if none exists (default is False) |
0 | Determines how to open the file: 0 for ASCII, -1 for Unicode, and -2 for the system default format) |
(Interested in learning more about the OpenTextFile method? Be sure to read the
technical documentation!)
At this point it might be a good idea to write all this script into a separate asp page and just "include" it
in our processing page. Let's call this page by the subroutine name, CreateHTMLPage.asp.
The completed code can be seen at the bottom of this article.
We are now ready for the final part of this article, putting it all together!
Read Part 3!
Sub CreateHTMLPage(getURL,postFile)
'###################################################
'# change the variables below to match your site #
'###################################################
'replace "getSite" variable to the address of your site
'to get the complete URL of the active file you want to run
Dim getPage
getPage = "http://www.yoursite.com" & getURL
'replace "postSite" to the physical address of your site
'to make a composite physical address of the "HTML" file
'you want to write to
Dim postPage
postPage = "C:\Inetpub\wwwroot\mysite" & postFile
'##################################################
'# end changes #
'##################################################
'variables for "ASPTear" component
Const Request_POST = 1
Const Request_GET = 2
'initiate component "ASPTear.dll"
Dim TearObj
Set TearObj = CreateObject("SOFTWING.ASPTear")
Response.ContentType = "text/html"
'Note on above line: you can actually skip it if your asp file
'that you are calling has a similar declaration in its HEAD part,
'like:
'<meta http-equiv="Content-Type"
content="text/html;
charset=iso-8859-1">
' to collect the output of the ASP page as a string
Dim strRetrieval
strRetrieval = TearObj.Retrieve(getPage, Request_GET, _
"", "", "")
'in case of error getting the page, output error
'this is a direct copy from the documentation of ASPTear
If Err.Number <> 0 Then
Response.Write "<b>
If Err.Number >= 400 Then
Response.Write "Server returned error: " & Err.Number
Else
Response.Write "Component/WinInet error: " & Err.Description
End If
Response.Write "<b>
Response.End
End If
Set TearObj = Nothing
'write to file...
Dim objFile, objTStream
Set objFile = Server.CreateObject("Scripting.FileSystemObject")
Set objTStream = objFile.OpenTextFile(postPage, 2, True, 0)
objTStream.Write(strRetrieval)
Set objTStream = Nothing
Set objFile = Nothing
End Sub
|