[返回]
中国计算机报2000年第55期

利用ASP建立自己的BBS

程清亮

  利用ASP,将Form的文本框中填写的东西记入文本文件,然后把该文件读出在Web页面上。利用这种方法,我们可以建立自己简单的BBS。

  要创建并且写入一个文本文件,可以使用FileSystemObject和TextStream对象。首先,我们可以创建一个FileSystemObject对象的事例,然后利用CreateTextFile()创建一个TextStream对象的事例,最后利用TextStream对象的WriteLine()方法来写入文件。以下是一个用ASP编写的BBS源程序。

  bbs.htm的源程序如下:

  〈html〉

  〈head〉

  〈title〉武朝尉的BBS〈/title〉

  〈base traget="main"〉

  〈/head〉

  〈body BGCOLOR="#FFFFFF"〉

  〈form method="POST" action="bbs.asp" target="main"〉

   〈div align="center"〉〈center〉〈table BORDER="0" width="607" cellspacing="0" cellpadding="0"〉

   〈tr〉

  〈td width="444"〉〈u〉〈strong〉姓名〈/strong〉〈/u〉〈input type="text" name="myname" size="39"〉〈/td〉

   〈td width="163"〉〈/td〉

   〈/tr〉

   〈tr〉

  〈td width="444" valign="top"〉〈u〉

  〈strong〉发言〈/strong〉〈/u〉〈textarea rows="2" name="mytalk"

  cols="38"〉〈/textarea〉〈/td〉

  〈td width="163"〉〈input type="submit" value="提交" name="B1"〉〈input type="reset"

  value="清除" name="B2"〉〈/td〉

   〈/tr〉〈/table〉

  〈/center〉〈/div〉

  〈/form〉〈/body〉

  〈/html〉

  bbs.asp源程序如下:

  〈html〉

  〈head〉

  〈meta http-equiv="Content-Type" content="text/html; charset=gb2312"〉

  〈title〉bbs--asp〈/title〉

  〈/head〉

  〈body〉

  〈%

  Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")

  Set MyTextFile=MyFileObject.OpenTextFile("d:\wucw\asptest\talk.txt",8,true)

  MyTextFile.WriteLine(Request.Form("myname")&&":"&&Request.Form("mytalk"))

  MyTextFile.Close %〉

  〈p align="center"〉

  〈font face="黑体" color="#FF0000" size="4"〉欢迎光临武朝尉的BBS〈/font〉〈/p〉

  〈%Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")

  Set MyTextFile=MyFileObject.OpenTextFile("d:\wucw\asptest\talk.txt")

  While NOT MyTextFile.AtEndOfStream

  Response.Write(MyTextFile.Readline)

  %〉

  〈p〉

  〈% WEND

  MyTextFile.Close %〉

  〈/p〉

  〈/body〉

  〈/html〉

  注意:这里没有用Response.Write(MyTextFile.Readall),而用Response.Write(MyTextFile.Readline)循环一行一行读出。使用Response.Write(MyTextFile.Readall)后,文章不换行,所以在每读一行后,用“〈p〉”换行。

  bbsmain.htm源程序如下:

  〈html〉

  〈head〉

  〈title〉main bbs〈/title〉

  〈/head〉

  〈frameset rows="70%,30%"〉

   〈frame name="main" src="http://flower/wucw/asptest/bbs.asp"〉

   〈frame name="footnotes" target="main" src="http://flower/wucw/asptest/bbs.htm"〉

   〈noframes〉

   〈body〉

   〈/body〉

   〈/noframes〉

  〈/frameset〉

  〈/html〉

  笔者在这里只是利用ASP对文本文件的写操作功能做一个简单的练习,供大家参考。