ASP中FSO的神奇功能 - 寫(xiě)文件
更新時(shí)間:2006年10月28日 00:00:00 作者:
作 者 : 甘冀平 ;
假設(shè)你想創(chuàng)建一個(gè)簡(jiǎn)單的留言簿,你可以建立一個(gè)數(shù)據(jù)庫(kù),在其中存儲(chǔ)用戶的信息。然而,如果并不需要數(shù)據(jù)庫(kù)的強(qiáng)大功能,使用FSO來(lái)存儲(chǔ)信息將節(jié)省你的時(shí)間和金錢。并且,一些ISP也許限制了web上的數(shù)據(jù)庫(kù)應(yīng)用。
假設(shè)你在一個(gè)表單中收集了一些用戶信息,這里是一個(gè)簡(jiǎn)單表單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")
迄今為止,還沒(méi)有新鮮的東西,無(wú)非是獲取表單域的值并且賦值到變量。下面出現(xiàn)了有趣的部分 - 寫(xiě)文件:
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方法返回一個(gè)TextStream對(duì)象,它是FSO模型中的另外一個(gè)對(duì)象。TextStream對(duì)象揭示了操作文件內(nèi)容的方法,比如寫(xiě)、讀一行、跳過(guò)一行。VB常量vbcrlf產(chǎn)生一個(gè)換行符。
在OpentextFile的命令參數(shù)中定義了TRUE,這就告訴了系統(tǒng),如果文件不存在,就創(chuàng)建它。如果文件不存在,并且沒(méi)有定義TRUE參數(shù),就會(huì)出錯(cuò)。
現(xiàn)在轉(zhuǎn)到目錄c: emp,打開(kāi)test.txt,你可以看到如下的信息:
User's name
User's home page
User's email
當(dāng)然,這些單詞可以被輸入在表單中的任何內(nèi)容所替換。
假設(shè)你想創(chuàng)建一個(gè)簡(jiǎn)單的留言簿,你可以建立一個(gè)數(shù)據(jù)庫(kù),在其中存儲(chǔ)用戶的信息。然而,如果并不需要數(shù)據(jù)庫(kù)的強(qiáng)大功能,使用FSO來(lái)存儲(chǔ)信息將節(jié)省你的時(shí)間和金錢。并且,一些ISP也許限制了web上的數(shù)據(jù)庫(kù)應(yīng)用。
假設(shè)你在一個(gè)表單中收集了一些用戶信息,這里是一個(gè)簡(jiǎn)單表單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")
迄今為止,還沒(méi)有新鮮的東西,無(wú)非是獲取表單域的值并且賦值到變量。下面出現(xiàn)了有趣的部分 - 寫(xiě)文件:
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方法返回一個(gè)TextStream對(duì)象,它是FSO模型中的另外一個(gè)對(duì)象。TextStream對(duì)象揭示了操作文件內(nèi)容的方法,比如寫(xiě)、讀一行、跳過(guò)一行。VB常量vbcrlf產(chǎn)生一個(gè)換行符。
在OpentextFile的命令參數(shù)中定義了TRUE,這就告訴了系統(tǒng),如果文件不存在,就創(chuàng)建它。如果文件不存在,并且沒(méi)有定義TRUE參數(shù),就會(huì)出錯(cuò)。
現(xiàn)在轉(zhuǎn)到目錄c: emp,打開(kāi)test.txt,你可以看到如下的信息:
User's name
User's home page
User's email
當(dāng)然,這些單詞可以被輸入在表單中的任何內(nèi)容所替換。
相關(guān)文章
一個(gè)實(shí)用的FSO-實(shí)時(shí)統(tǒng)計(jì)在線人數(shù)
一個(gè)實(shí)用的FSO-實(shí)時(shí)統(tǒng)計(jì)在線人數(shù)...2006-10-10asp下用fso和ado.stream寫(xiě)xml文件的方法
用asp來(lái)生成xml文件,一開(kāi)始我用fso,只用了寫(xiě),同時(shí)把讀文件的辦法也給大家2008-10-10使用FSO把文本信息導(dǎo)入數(shù)據(jù)庫(kù)
使用FSO把文本信息導(dǎo)入數(shù)據(jù)庫(kù)...2006-10-10用XML+FSO+JS實(shí)現(xiàn)服務(wù)器端文件的
用XML+FSO+JS實(shí)現(xiàn)服務(wù)器端文件的...2006-10-10