November 30th, 2006, 04:46 AM
Moderator From Beyond
Join Date: Sep 2004
Location: Israel
Classic ASP: caching ASP as HTML files.
use the below code to "cache" ASP files as HTML meaning read
the output of given ASP page and save this output as html file
on the server.
Code:
<% Option Explicit %>
<%
Dim objXMLHTTP, strURL, strHTML
Dim binData, objFSO, strFileName
Dim strFilePath, objFile
strURL="http://www.yourdomain.com/page.asp"
Response.Write("caching page " & strURL & "...<br />")
'initialize xmlhttp component:
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
'send request:
objXMLHTTP.Open "GET", strURL, False
objXMLHTTP.Send
'read raw data:
binData = objXMLHTTP.ResponseBody
Set objXMLHTTP = Nothing
Response.write("page found, size is " & LenB(binData) & " bytes<br />")
'convert binary to ascii data:
strHTML = RSBinaryToString(binData)
'build proper file name:
strFileName = GetFileNameNoExtension(strURL) & ".html"
Response.Write("writing contents to local file " & strFileName & "...<br />")
'initialize file system component:
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'build file path:
strFilePath = Server.MapPath(strFileName)
'create the file and write contents:
Set objFile = objFSO.CreateTextFile(strFilePath)
objFile.Write(strHTML)
objFile.Close
Set objFSO = Nothing
Response.Write("done. <a href=""" & strFileName & """>" & strFileName & "</a>")
Function GetFileNameNoExtension(strPath)
Dim index, strFileName
index = InStrRev(strPath, "\")
If index=0 Then
index = InStrRev(strPath, "/")
End If
If index=0 Then index = 1
strFileName = Mid(strPath, index+1, Len(strPath))
index = InStrRev(strFileName, ".")
If index=0 Then index = Len(strFileName)+1
GetFileNameNoExtension = Mid(strFileName, 1, index-1)
End Function
Function RSBinaryToString(xBinary)
'Antonin Foller, http://www.motobit.com
'RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string)
'to a string (BSTR) using ADO recordset
Dim RS, LBinary, Binary
Const adLongVarChar = 201
'MultiByte data must be converted To VT_UI1 | VT_ARRAY first.
If vartype(xBinary)=8 Then
Binary = MultiByteToBinary(xBinary)
Else
Binary = xBinary
End If
Set RS = CreateObject("ADODB.Recordset")
LBinary = LenB(Binary)
If LBinary>0 Then
RS.Fields.Append "mBinary", adLongVarChar, LBinary
RS.Open
RS.AddNew
RS("mBinary").AppendChunk Binary
RS.Update
RSBinaryToString = RS("mBinary")
Else
RSBinaryToString = ""
End If
End Function
Function MultiByteToBinary(MultiByte)
'© 2000 Antonin Foller, http://www.motobit.com
' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
' Using recordset
Dim RS, LMultiByte, Binary
Const adLongVarBinary = 205
Set RS = CreateObject("ADODB.Recordset")
LMultiByte = LenB(MultiByte)
If LMultiByte>0 Then
RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
RS.Open
RS.AddNew
RS("mBinary").AppendChunk MultiByte & ChrB(0)
RS.Update
Binary = RS("mBinary").GetChunk(LMultiByte)
End If
MultiByteToBinary = Binary
End Function
%>
the code is using XMLHTTP to read the contents of the page and
FSO to write those contents into HTML file with same name.
note: for the code to work properly, give full URL!
Happy Programming!
Comments on this post
KiReSt
agrees: Nice, just what I am looking for ..soon
Last edited by Shadow Wizard : November 30th, 2006 at 04:50 AM .