关于我们 广告服务 社区论坛
设为首页 加入收藏

行业新闻
服 务 器
模版下载
建站指南
冲浪宝典
办公软件
网站运营
操作系统
QQ 专题
网页制作
安全防御
视频教程
网络编程
SEO专区
软件下载
图像设计
Cisco
网页特效
Wap 技术
联盟赚钱
网页素材
 首页 | 企业建站 | 网页制作 | 网站运营 | 网络编程 | 图像设计 | 冲浪宝典 | 操作系统 | SEO专区 | 联盟赚钱 | Cisco

欢迎来到e天下网络首页>>网络编程>>ASP>>正文|对ASP动态包含文件方法的改进

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

[ 来路:21kn.com    时间:2007-7-8 0:46:47    点击: ]

 

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

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

::::站长友情提示:多花一分钟学点什么都好::::

 

上一篇:全面剖析XMLHttpRequest对象  下一篇:用ASP动态生成JS的表单验证代码

 ::热点信息::

 

= = 免责声明 = =

① 欢迎转载我网所刊信息,请注明“来源:E天下网络”。
② 凡本网注明“来源:XXX(非E天下网络)”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如因作品内容、版权和其它问题需要同本网联系的,请在30日内进行。
※联系方式:Airtofly@163.com

::推荐文章::

 

ASP教程:详细学习ASP的内置对象

::图像设计::

 

动态图片搜索家——GIF RUNN
软件搜索利器——FileFerret
实例说明构图要讲规律
Character Builder让你尽展靓
全景图速成者Cool360
三维模型速成工具——Canoma
剪贴专家SmartBoard 32
新世纪的图像处理利器——Ph
更多内容..

 

 

关于我们 广告服务 友情链接 合作伙伴 社区论坛 免责声明

Copyright © 2007   21kn.com Inc. All rights reserved.e天下网络工作室

网站白天客服QQ:26875416 (非24小时)  合作QQ:597004688    粤ICP备06026423号