使用范例需要两个条件:
1.(读写都需要)服务器开启父目录
2.(写文件需要)服务器开启写入权限
(一)读取根目录下的所有文件及文件夹信息并输出
'filelist.asp
'作用:读取path目录下所有子目录和文件并以表格输出其信息
'path为读取文件相对根目录的路径,如根目录是"/"
<%
path = request.QueryString("path") '读取路径参数
if path <> "" then path = path else path = "/" end if '不传参数时默认读根目录
rootpath = Server.MapPath(path) '获得path路径所在的服务器完整路径,需要开启父目录
Set fileObj = Server.CreateObject("Scripting.FileSystemObject") '创建FSO对象
Set root = fileObj.GetFolder(rootpath) '创建文件夹对象
For each folder in root.subfolders '读取文件夹对象下的所有子目录并显示其信息
if path<>"/" then tmppath = path&"/"&folder.name else tmppath = path&folder.name end if
Response.Write("")
Response.Write("")
next
For each fileitem in root.files '读取目录下所有文件并显示其信息
if path<>"/" then tmppath = path&"/"&fileitem.name else tmppath = path&fileitem.name end if
'response.Write(tmppath)
Response.Write("")
Response.Write("")
next
Set fileObj = nothing
Set root = nothing
Set folder = nothing
Set fileitem = nothing
%>
名称 | 父目录 | 属性 | 大小 | 日期 | | |
"&folder.name&" | "&folder.ParentFolder.name&" | 目录 | | "&folder.DateCreated&" | 打开 | 删除 |
"&fileitem.name&" | "&fileitem.ParentFolder.name&" | 文件 | "&fileitem.size&" byte | "&fileitem.datelastmodified&" | 打开 | 删除 |
(二)读取某个文件内容并输出到文本区域
<%
fid = Request.QueryString("fid") 'fid为文件相对根目录的路径,如根目录下的index.htm的fid = /index.htm
%>
<%
Set fso = Server.CreateObject("scripting.FileSystemObject") '创建FSO对象
Set fileObj = fso.opentextfile(server.mappath(fid),1,true) '创建文件读取对象,用于字符文件
filecontent = fileObj.readall '用文件读取对象读出文件内容
%>
<%
Set fileObj = nothing
Set fso = nothing
%>
(三)使用FSO写入文件
<%
content = Request.Form("content") '表单提交的数据
fid = request.QueryString("fid") '请求参数,指向文件存取相对根目录的路径
'response.Write("fid = "&fid) '调试使用,输出请求参数
'response.Write("content = "&content) ’调试使用,输出表单提交数据
Set fso = Server.CreateObject("scripting.FileSystemObject") '创建FSO对象
Set fileObj = fso.opentextfile(server.mappath(fid),2,true) '使用FSO创建文件写入对象
fileObj.Write content '向文件写入数据,覆盖形式写入
fileObj.close '推送内容写入并关闭写入通道
response.Write("保存成功")
Set fileObj = nothing
Set fso = nothing
%>
详细代码:
<%
strs="序号"&Chr(9)&"用户名"&Chr(9)&"姓名"&Chr(9)&"注册日期"&Chr(9)&"开课日期"&Chr(9)&"是否已支付"&Chr(9)&"课程金"&Chr(9)&"商品金"&Chr(9)&"学习卡"&Chr(9)&"管理员"&Chr(9)&"省份"&Chr(9)&"来自"&Chr(9)&"是否包含退费班"&vbcrlf
set rs3=server.CreateObject("adodb.recordset")
rs3.open sql,conn,3
for i=1 to rs3.recordcount
strs=strs&i&Chr(9)&trim(rs3("username"))&Chr(9)&trim(rs3("name"))&Chr(9)&trim(rs3("addtime"))&Chr(9)&trim(rs3("order_time"))&Chr(9)&trim(rs3("is_pay"))&Chr(9)&trim(rs3("select_total_amount"))&Chr(9)&trim(rs3("order_total_amount"))&Chr(9)&trim(rs3("pay_total_amount"))&Chr(9)&trim(rs3("admin_name"))&Chr(9)&trim(rs3("province"))&Chr(9)&trim(rs3("fromsite"))&Chr(9)&trim(rs3("is_refund"))&vbcrlf
rs3.movenext
next
content =strs '表单提交的数据
fid = "/new_admin/statistics/Excels/"&mytime&".xls" '请求参数,指向文件存取相对根目录的路径
'response.Write("fid = "&fid) '调试使用,输出请求参数
'response.Write("content = "&content) ’调试使用,输出表单提交数据
Set fso = Server.CreateObject("scripting.FileSystemObject") '创建FSO对象
Set fileObj = fso.opentextfile(server.mappath(fid),2,true) '使用FSO创建文件写入对象
fileObj.Write content '向文件写入数据,覆盖形式写入
fileObj.close '推送内容写入并关闭写入通道
response.Write("保存成功!")
Set fileObj = nothing
Set fso = nothing
%>