`
txf2004
  • 浏览: 6867923 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ASP动态包含文件的改进方法,ASP动态include

阅读更多

ASP 本身不支持动态包含文件,现在的动态包含是通过 FSO 把被包含的文件合并到主文件里再运行。以下也有把形如 <!--#include file="filename.asp" --> 的普通包含文件方式称作“传统引用”,用函数实现的动态包含文件称作“动态引用”。常见的程序如下:

以下为引用的内容:
Function include(filename)
Dim re,content,fso,f,aspStart,aspEnd


set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
set f=nothing
set fso=nothing

set re=new RegExp
re.pattern="^\s*="
aspEnd=1
aspStart=inStr(aspEnd,content,"<%")+2
do while aspStart>aspEnd+1
Response.write Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content,"%\>")+2
Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))
aspStart=inStr(aspEnd,content,"<%")+2
loop
Response.write Mid(content,aspEnd)
set re=nothing
End Function



使用范例:include("youinc.asp")

但这处函数在处理补包含的文件中还有包含文件时就不灵了。我在以上函数的基础上改进出来如下函数,在被包含文件中还有普通的包含文件 <!--#include file="filename.asp" --> 也可正常运行。

Function includeconvert(oRegExp, strFilename, strBlock)
Dim incStart, incEnd, match, oMatches, str, code
&apos;用提取ASP代码的相同方式提取出include 部分的文件名,其余部分原样输出
code = ""
incEnd = 1
incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 &apos;要找个目标字符串<!--#include 正好是13个字符,所以要+13
Do While incStart>incEnd+12 &apos;两个引用间距最小就是连续的--><--#,incStart是从<!--#include起数13个字符,所以要比前一个incEnd要至少多 13-1 得到的>incEnd+12的条件
str = Mid(strBlock,incEnd,incStart-incEnd-13)
str = Replace(str, """", """""") &apos;把单个双引号换成两个双引号
str = Replace(str, VbCr, "")
str = Replace(str, VbLf, "")
str = Replace(str, VbCrLf, "")
code = code & VbCrLf & "Response.Write """ & str & """"
incEnd=InStr(incStart,strBlock,"-->")+3
oRegExp.pattern="(\w+)=""([^""]+)""" &apos;匹配 file="filename.ext" 或 virtual="virtualname.ext",捕捉类型及文件名两个子串
Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3))
Set match = oMatches(0) &apos;确定只有一组捕捉时,要得到这一组匹配的子串,可以这样做,省去用 For Each match In oMatches …… Next
code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) &apos;Mid(filename, 1, InStrRev(filename, "/")) 是在被引用的子文件名有路径时,把路径提取出来,加在子文件中传统引用的文件名前面,以找到正确的打开文件路径,因为动态引用时的文件路径是相对主文件而言的。要第二个匹配子串用SubMatches(1)

以下为引用的内容:
incStart = InStr(incEnd,strBlock,"<!--#include ")+13
Loop
str = Mid(strBlock,incEnd)
str = Replace(str, """", """""") &apos;把单个双引号换成两个双引号
str = Replace(str, VbCr, "")
str = Replace(str, VbLf, "")
str = Replace(str, VbCrLf, "")
code = code & VbCrLf & "Response.Write """ & str & """"
includeconvert = code
End Function
Function include(filename)
Dim re, content, fso, f, aspStart, aspEnd, code
Set fso=CreateObject("scripting.FileSystemObject")
Set f=fso.OpenTextFile(Server.MapPath(filename))
content=f.ReadAll
f.close
Set f=nothing
Set fso=nothing


code = ""
aspEnd=1
aspStart=InStr(aspEnd,content,"<%")+2
Set re=new RegExp
Do While aspStart>aspEnd+1
&apos;传统引用<!--#inclde 肯定是在ASP代码段以外的,所以先转。
code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))
aspEnd=InStr(aspStart,content,"%\>")+2
re.pattern="^\s*=" &apos;这段正则替换原来是把 <% = str % > 换回成标准的 <%Response.Write str % >
code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") &apos;ASP块前面再加回车换行,以避免连接块之间多个 Response.Write在同一行的错误
aspStart=InStr(aspEnd,content,"<%")+2
Loop
code = code & includeconvert (re, filename, Mid(content,aspEnd))
Set re=nothing
include = code
End Function



方便起见,以上函数最终返回的是整合了包含文件的整个 ASP 代码,使用时还要再用 Execute 执行之,即使用时需要:Execute(include("file.asp"))。

以上函数对被包含文件与主文件同一路径时测试通过,未对被包含文件与主文件路径不同的情况做进一步容错,时间有限,欢迎有兴趣的朋友提出意见和改进。

分享到:
评论
1 楼 lyby 2011-04-12  
rem Response.write request("file")

set filesUrl=request("file")
fileArray=Split(filesUrl,",")
For I = Lbound(fileArray) to Ubound(fileArray)
rem Response.Write fileArray(I) & "<br>"
include("\./"+fileArray(I))
Next

rem include("\./js/cmo.js")
rem include("getx86List.html")

在最下面增加这个,就可以多文件include

相关推荐

    asp动态include文件

    一个asp动态include文件,分享给大家,呵呵,欢迎探讨!

    ASP动态include文件

    经常有这样的要求,根据不同的需求要求include不同的文件如各个人的不同设置,所以要求能动态include文件受&lt;! #include file=”filename.asp” –&gt; 宏限制  必须存在该文件并且会预先编译(不管前面是否加以条件...

    ASP动态包含技术DEMO(asp_dynamic_include_demo)

    ASP动态包含技术DEMO(asp_dynamic_include_demo),演示如何动态包含另外的asp演示Demo

    asp动态include文件,方便多模板的实现

    asp动态include文件,方便多模板的实现

    ASP中利用execute实现动态包含文件的方法

    ASP中,include file/virtual 是优先脚本代码处理的,所以无法使用include动态包含ASP文件。我们可以使用Execute函数动态执行所需代码。 方法: Execute(ASP代码) 例子:(vbCrLf为换行符) 代码如下:Execute(...

    【ASP.NET编程知识】浅谈ASP.NET的include的使用方法.docx

    【ASP.NET编程知识】浅谈ASP.NET的include的使用方法.docx

    asp.net 包含文件的方法

    在ASP.NET包含文件的方法有: 1.&lt;&#37; Response.WriteFile&#40;“skin/default/footer.txt”&#41;%&gt; 2.&lt;&#37; server.execute(“skin/default/footer.txt”)%&gt; 3.StreamReader 对象将包含文件写到 HTTP 内容流...

    浅谈ASP.NET的include的使用方法

    我们学过的Code分离到不同文件的方法,主要包括: 程序集.dll、&lt;inherits&gt;.cs、[removed].cs、用户控件.ascx、include、Response.WriteFile&#40;&#41; 程序集.dll:这是最高级的方法,它引用的是一个被编译为IL的DLL...

    分享一个好东东,动态Include文件 (Dynamic File Includes)

    早在03年就在蓝色理想上看到过动态... 以前试的一些动态Include代码,都无法Include一个类,甚至函数~~~又或者Include文件中的Include无法被包含… 现在这个鬼佬(dselkirk)写的类可以为我们做到这些了~~~ 代码如下:

    ASP Code Migrator v1.5.2(ASP代码升级到ASP.NET工具)

    主要完成的步骤如下:从指定的站点入口文件出发,扫描页面文件中的所有超链接,然后把asp文件转换成.aspx文件,把仅被include的asp或.inc文件转换成.ascx文件(注意默认情况下,不会转换成用户控件,只是后缀名为...

    shtml的include网页页面支持

    而html的框架(frame与iframe)虽然可以用来包含首页与页脚文件,但使 用frame与iframe的概念与动态页面(asp,jsp,php等)的include那样不同,frame与iframe的主体页面与被包括的页的 不是一个页面,而通过include的方式...

    网站asp 源码

    系统管理: 你的域名/admin/admin_login.asp 帐号:admin 管理密码:...配置文件/include/config.asp 数据文件/include/conn.asp 数据文件/include/conig.asp 首页新闻调用:(25)%&gt; 25指分类的ID号 首页扩展调用:

    ASP抓蜘蛛的代码生成日志文件

    ASP抓蜘蛛的代码生成日志文件将本代码加入要统计的文件中,其中路径改为你的路径 &lt;!--#include file="路径/stat.asp" --&gt; 蜘蛛访问后系统会生成日志文件放在/目录log/data/下。 察看日志请访问:http://网址/目录/...

    微标ASP无组件上传类 v1.3(无刷新、多文件上传,并且可查杀木马,utf-8格式)

    查杀图片木马原理:一些图片会被不法分子插入些可执行的asp代码,在被include的时候悄悄执行,我们将图片以文本方式进行检查,判断是否包含某些关键字,如果包含就停止上传,总而提高了服务器的安全。 本来想打包个...

    Asp框架AspBox v1.3.2a

    AspBox是一个方便快速开发ASP框架,AspBox提供了大量实用的ASP通用过程及方法和子类,可以简化大部分的ASP操作。AspBox还可以进行拓展子类对象以增强自身功能。封装严谨,层层嵌套,提高了代码重复利用多次利用。 ...

    智睿ASP院校培训学校系统 v6.3.0.rar

    智睿ASP培训学校系统,模块包括:网站首页 学校概况 校园新闻 德育教学 校园风彩 求贤纳士 成绩查询 网上报名 联系我们 会员中心 校园信箱等,采用当前学校网站的适用性,智睿学校网站管理系统永久使用,终身免费。...

    asp #include命令

    asp #include命令

    asp.net服务器端指令include的使用及优势介绍

    – #include file|virtual=”filename” –&gt;这样的指令,msdn中的名词解释是:将指定文件的内容插入 ASP.NET 文件中,包括网页(.aspx 文件)、用户控件文件(.ascx 文件)和 Global.asax 文件。插入静态文件这个...

    无组件ASP文件上传源代码

    无组件ASP文件上传源代码 记得在建立一个文件夹"updata" saveannounce_upload.asp 上传页 ------------------------------------ &lt;html&gt; &lt;head&gt; &lt;style type="text/css"&gt; body {font-size:9pt;} input {...

Global site tag (gtag.js) - Google Analytics