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

  • 来源: 互联网 作者: 若水   2008-03-17/11:40
  • 假设你想创建一个简单的留言簿,你可以建立一个数据库,在其中存储用户的信息。然而,如果并不需要数据库的强大功能,使用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")

    迄今为止,还没有新鲜的东西,无非是获取表单域的值并且赋值到变量。下面出现了有趣的部分 - 写文件:

    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参数,就会出错。

    现在转到目录c: emp,打开test.txt,你可以看到如下的信息:

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

    当然,这些单词可以被输入在表单中的任何内容所替换。


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多