网络编程
位置:首页>> 网络编程>> Asp编程>> ASP生成静态网页各种方法收集整理(3)

ASP生成静态网页各种方法收集整理(3)

  发布时间:2008-02-18 19:25:00 

标签:静态,静态网页,fso,xmlhttp,adodb


6.下面是利用XMLHTTP将 * 页生成静态网页的一段简单代码。

如一个正常的index.asp页面,并且用ASP代码调出数据库中的内容,另建一个makehtml.asp的页面,加入一个textarea域,假设为name="body",将index.asp在

textarea里调出来,如: 
<textarea name="body"><!--#include file="index.asp"--></textarea>

将这个textarea包含在表单中,在接收表单页用创建FSO对象,如下生成index.html文件!

<% 
filename="../index.html" 
if request("body")<>"" then 
set fso = Server.CreateObject("Scripting.FileSystemObject") 
set fout = fso.CreateTextFile(server.mappath(""&filename&"")) 
fout.write request.form("body") 
fout.close 
set fout=nothing 
set fso=nothing 
end if 
%> 

这样index.html文件就生成了,连模板都用不着,只要将正常情况下使用的ASP文件读取到textarea里就可以了,目前尚未发现问题!当然前提是服务器要支持FSO。

开启FSO权限 在 开始-“运行”中执行regsvr32.exe scrrun.dll即可。如想关闭FSO权限,在上述命令中加/u参数。注册表中的键值位置:HKEY_CLASS_BOOT\F.S.O .FSO中有个方法是CreateFolder,但是这个方法只能在其上一级文件夹存在的情况下创建新的文件夹,所以我就写了一个自动创建多级文件夹的函数,在生成静态页面等方面使用非常方便。函数:


’ -------------------------------- 
’ 自动创建指定的多级文件夹 
’ strPath为绝对路径 
Function AutoCreateFolder(strPath) ’ As Boolean 
        On Error Resume Next 
        Dim astrPath, ulngPath, i, strTmpPath 
        Dim objFSO 
        If InStr(strPath, "\") <=0 Or InStr(strPath, ":") <= 0 Then 
                AutoCreateFolder = False 
                Exit Function 
        End If 
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
        If objFSO.FolderExists(strPath) Then 
                AutoCreateFolder = True 
                Exit Function 
        End If 
        astrPath = Split(strPath, "\") 
        ulngPath = UBound(astrPath) 
        strTmpPath = "" 
        For i = 0 To ulngPath 
                strTmpPath = strTmpPath & astrPath(i) & "\" 
                If Not objFSO.FolderExists(strTmpPath) Then 
                        ’ 创建 
                        objFSO.CreateFolder(strTmpPath) 
                End If 
        Next 
        Set objFSO = Nothing 
        If Err = 0 Then 
                AutoCreateFolder = True 
        Else 
                AutoCreateFolder = False 
        End If 
End Function 

调用方法:

MyPath = "C:\a\b\c\" 
If AutoCreateFolder(MyPath) Then 
        Response.Write "创建文件夹成功" 
Else 
        Response.Write "创建文件夹失败" 
End If

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com