凹丫丫网络社区's Archiver

34008950bb 发表于 2008-3-26 13:15

ASP中FSO的神奇功能-写文件

[table][tr][td][/td][/tr][/table]假设你想创建一个简单的留言簿,你可以建立一个数据库,在其中存储用户的信息。然而,如果并不需要数据库的强大功能,使用FSO来存储信息将节省你的时间和金钱。并且,一些ISP也许限制了web上的数据库应用。假设你在一个表单中收集了一些用户信息,这里是一个简单表单HTML代码:
< html>
< body>
< form action="formhandler.asp" method="post">
< input type="text" size="10" name="username">
< input type="text" size="10" name="homepage">
< input type="text" size="10" name="Email">
< /form>
< /body>
< /html>
再看看formhandler.asp中处理表单的代码:
< %
' Get form info
strName = Request.Form("username")
strHomePage = Request.Form("homepage")
strEmail = Request.Form("Email")

' create the fso object
Set fso = Server.CreateObject("Scripting.FileSystemObject")

   迄今为止,还没有新鲜的东西,无非是获取表单域的值并且赋值到变量。下面出现了有趣的部分 - 写文件:[url=http://765.com.cn/sort.asp?sort_id=10][color=#ffffff]虚拟主机[/color][/url]

path = "c: emp est.txt"
ForReading = 1, ForWriting = 2, ForAppending = 3

' open the file
set file = fso.opentextfile(path, ForAppending, TRUE)

' write the info to the file
file.write(strName) & vbcrlf
file.write(strHomePage) & vbcrlf
file.write(strEmail) & vbcrlf

' close and clean up
file.close
set file = nothing
set fso = nothing


   回想一下,OpenTextFile方法返回一个TextStream对象,它是FSO模型中的另外一个对象。TextStream对象揭示了操作文件内容的方法,比如写、读一行、跳过一行。VB常量vbcrlf产生一个换行符。

  在OpentextFile的命令参数中定义了TRUE,这就告诉了系统,如果文件不存在,就创建它。如果文件不存在,并且没有定义TRUE参数,就会出错。[url=http://765.com.cn/sort.asp?sort_id=10][color=#ffffff]虚拟主机[/color][/url]
现在转到目录c: emp,打开test.txt,你可以看到如下的信息:

User's name
User's home page
User's email

TroyHorse 发表于 2008-4-13 17:43

学习了,楼主辛苦了!!!

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2007 Comsenz Inc.