用asp技术实现在web网页上浏览目录及文件
周仰平
摘要 目前在许多单位里,象word、excel或wps该类文档文件已成为重要的数据信息资源,在intranet web平台上如何高效率地管理这些信息资源是一个需要解决的问题。本文是利用asp技术开发一个基于web的应用程序,实现在web页面上浏览目录文件,很轻易地做到诸如word、excel和wps等文档文件的查阅和下载,大大地减少信息维护的工作量。
关键词 asp web 文档文件
前言:在intranet平台上,客户用web浏览器从服务器下载过来的信息是以html标记构成的页面,也就是平常所说的网页,页面一般分为两类,一类是简单的静止不变的页面,包括文字、表格和静态图像等信息,这种页面可通过编写html标记组成的文本文件或采用页面制作工具如frontpage软件来实现;另一类是动态的具有交互性和智能化的页面,这类页面实现起来比较复杂,需要将html标记、activex控件及基于客户端和服务端的脚本语言(如vbscript、javascript)组合在一起构成web应用程序,web 程序经过服务端和客户机运行处理才得到页面。制作页面虽然有专用的页面制作工具来做,但是,要完成页面制作需掌握一定的专业知识,如html标记语言,如果要制作动态的有交互功能的页面,要求就更高了,需要专业人员才能做到。但是,目前在许多企事业单位中,数据信息的表示形式许多是文档文件,如word、excel、wps和txt等文档,这些文档文件已成为重要的数据信息资源,各部门之间的数据共享也是通过查询和拷贝彼此的文档文件来实现的。如果文档文件不作任何转换,就能在web浏览器中被查询和下载,那么信息处理的效率将会提高,大大地减少信息维护的工作量。
本文是运用asp技术实现在web页面浏览文件夹及文件,通过超连接功能查看相关的activex文档信息,其功能类似于ftp服务,但比ftp服务性能更好,一、界面更友好,同web页面无缝地连接;二、容易控制,安全性好,可以做到该需要查看的文档才显示在页面上,不该看的文档不显示。
二、asp技术及其对象
从iis3.0开始,microsoft推出了active server pages,既asp技术,asp能将html页面、脚本语言(vbscript、javascript)和activex控件有机地组合起来,创建具有动态的、交互式的、高效率的页面和基于web数据库的功能强大的服务器应用程序。
asp还有一个特点是其拥有功能强大的内置对象,对象之中又有很多的方法和属性。在用asp开发web应用程序时,能够调用其对象及其方法,不但能提高编程效率,而且程序编码更优化。
1、asp包括了六个内置对象:
request 对象 是从客户端取得信息
response 对象 将信息送给客户端
session 对象 存储在一个session内的用户信息
application对象 在一个asp-application内让不同的客户端共享信息
server 对象 提供一些服务端使用的方法。在本文的asp程序中运用了该对象的 几个方法,在后面将对其作具体地说明。
objectcontext对象 可以配合microsoft transaction server进行分布式事务处理
2、server对象的重要方法
createobject方法 createobject方法用于创建已注册到服务器上的activex组件变量实例。比如在asp程序中想要浏览文件夹及文件,必须先用createobject方法建立filesystemobject对象的变量实例fs,然后调用对象变量fs的方法以获取文件夹(folder)及文件(file),下面是获取文件夹为 /dcw 的folder对象的asp代码:
‘先建立filesystemobject
set fs=server.createobject(“scripting.filesystemobject”)
‘获取 /dcw文件夹的实际路径
fullpath=server.mappath(“/dcw”)
‘建立 /dcw文件夹所对应的 folder对象
set fd=fs.getfolder(fullpath)
mappath方法 转换web server 虚拟路径为服务器端实际路径。如在iis web服务器上假设有一实际路径为c:\财务部,在建立web 虚拟路径时取名为 dcw,书写格式为 /dcw,事实上 server.mappath(“/dcw”)就是它的实际路径 c:\财务部。一般地,在 web应用中浏览网页时使用的网址就是虚拟路径,在文件存取过程中使用的是实际路径,如前面提到的 set fd=fs.getfolder(fullpath)语句里,fullpath就表示为实际路径
urlencode 方法 根据url编码原则,将url 中的特殊字符编码为字符串
htmlencode方法 使用htmlencode方法编码为 ascii形式的html文件
三、实现过程
客户端向服务端传送请求信息
客户向服务端传送的请求信息是一个asp文件listf.asp和三个参数in_folder,out_file,prompt。
in_folder 表示为要浏览的文件夹
out_file 表示退出返回到的页面
prompt 表示在列出文件夹的页面上的提示信息
以浏览 “c:\财务部”文件夹信息为例,其web虚拟目录为“/dcw”,程序退出返回到主页,主页的虚拟路径可表示为“/”,提示信息为“财务数据信息”。在 web 程序中加入下面一条语句就可以查看“c:\财务部”文件夹下的所有子文件夹及文件。
<a href=listf.asp?in_folder=/dcw&out_file=/&prompt=财务数据信息>财务文件信息</a>
‘文件开始
<html><body bgcolor=”#c0c0c0”>
<%
‘2、获取参数信息
folder=request(“folder”)
if folder=”” then
in_folder=request(“in_folder”)
out_file=request(“out_file”)
prompt=request(“prompt”)
if in_folder<>”” and out_file<>’’ then
session(“in_folder”)=in_folder
session(“out_file”)=out_file
session(“prompt”)=prompt
end if
folder=session(“in_folder”)
prompt=session(“prompt”)
‘ 输出文件夹名和提示信息
response.write “<h2 align=””” & “center” & “””>” & prompt & “系列文件目录<hr></h2>”
else
subfolder=folder
folder=session(“in_folder”) & folder
‘输出文件夹名和提示信息
response.write “<h2 align=””” & “center” & “””>” & mid(ucase(replace(subfolder,”/”,”_”)),2) & “<hr></h2>”
end if
asppath=request.servervariables(“path_info”)
‘3、建立文件夹对象变量实例 fd
set fs= server.createobject(“scripting.filesystemobject”)
set fd= fs.getfolder(server.mappath(folder))
‘4、构造超连接函数
‘函数unmappath是将实际路径转换为虚拟路径
function unmappath(path)
unmappath=replace(mid(path,len(server.mappath(“in_folder”)))+1),”\”,”/”)
end function
‘函数makefolderhref 构造文件夹超连接
function makefolderhref(asppath,path)
urlpath=serverencode(unmappath(path))
makefolderhref=”<a href=””” & asppath & “?folder=” & urlpath & “””>”
end function
‘函数makefilehref 构造文件超连接
function makefilehref(path)
makefilehref=”<a href=””” & session(“in_folder”) & server.htmlencode(unmappath(path)) & “””>”
end function
‘5、返回到上一层文件夹或退出到主页
if folder<session(“in_folder”) then
‘有上一层文件夹
href_path=makefolderhref(asppath,fd.parentfolder.path)
response.write href_path & “[上一层文件夹]</a><br>”
else
response.write “<a href=””” & session(“out_file”) & “””>返回主页</a><br>”
end if
‘6、列出文件夹和文件
‘ 列出文件夹,文件夹为temp不显示出来
response.write “<table width=600 align=center border=1>”
ii=1
for each sfd in fd.subfolders ‘获取 fd 中的各个子文件夹subfolder
if sfd.name<>”temp” then
if ii=1 then
response.write “<tr><td>”
href=”img src=folder.gif align=texttop>”
href=href &makefolderhref(asppath,sfd.path)
response.write href & sfd.name & “</a><br>”
response.write “</td>”
ii=ii+1
else
response.write “<td>”
href=”img src=folder.gif align=texttop>”
href=href &makefolderhref(asppath,sfd.path)
response.write href & sfd.name & “</a><br>”
response.write “</td></tr>”
ii=1
end if
end if
next
if ii=2 then
response.write “<td> </td></tr>”
end if
response.write “</table>
‘以下为输出文件,扩展名为doc、xls、wps和txt的文档才显示出来,但扩展名不列出
response.write”<center><hr>以下为文件</center><br><p>”
response.write”<table width=600 align=center border=1>”
ii=1
for each f in fd.files
pos=instrrec(f.path,”.”)
if pos>0 then
ext=mid(f.path,pos+1)
else
ext=””
end if
if lase(ext)=”doc” or lcase(ext)=”xls” or lcase(ext)=”wps” or lcase(ext)=”txt” then
pos=instrrec(f.name,”.”)
if pos>0 then
noext_file=left(f.name,pos-1)
end if
if ii=1 then
response.write “<tr><td>”
href=”<img src=file.gif align=texttop>” & makefilehref(f.path)
response.write href & noext_file & “</a><br>”
response.write “</td>”
ii=ii+1
else
response.write “<td>”
href=”<img src=file.gif align=texttop>” & makefilehref(f.path)
response.write href & noext_file & “</a><br>”
response.write “</td></tr>”
ii=1
end if
end if
next
if ii=2 then
response.write “</td></tr>”
end if
response.write “</table><br>”
%>
</body></html> ‘文件结束
四、结论
本文的应用程序在window nt4.0,iis4.0和ie4所构成的intranet web平台上运行,既可以单独调用,又可以嵌入到其它的web程序或html页面中,使用web浏览器很方便地查看到文件目录信息,并可以下载文件。但要注意的是,该程序适合于局域网下使用,对于远程调用不太合适,另外,还要求客户端安装了office、wps97等软件。
