ASP中FSO的神奇功能(2)
作者:甘冀平 来源:未知 添加时间:2006-5-21 10:31:03<P> 假设你在一个表单中收集了一些用户信息,这里是一个简单表单HTML代码:</p>
<pre>< 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>
</pre>
<P> 再看看formhandler.asp中处理表单的代码:</p>
<pre>< %
"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")
</pre>
<P> 迄今为止,还没有新鲜的东西,无非是获取表单域的值并且赋值到变量。下面出现了有趣的部分 - 写文件:</p>
<pre>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
</pre>
<P> 回想一下,OpenTextFile方法返回一个TextStream对象,它是FSO模型中的另外一个对象。TextStream对象揭示了操作文件内容的方法,比如写、读一行、跳过一行。VB常量vbcrlf产生一个换行符。</p>
<P> 在OpentextFile的命令参数中定义了TRUE,这就告诉了系统,如果文件不存在,就创建它。如果文件不存在,并且没有定义TRUE参数,就会出错。</p>
<P> 现在转到目录c: emp,打开test.txt,你可以看到如下的信息:</p>
<pre>User"s name
User"s home page
User"s email
</pre>
<P> 当然,这些单词可以被输入在表单中的任何内容所替换。</p>
站内搜索