ASP 讀寫文件FSO,adodb.stream


例如靜態化頁面的時候

總結:用server.CreateObject("adodb.stream") 來讀寫比較好,可避免亂碼和讀取到多余的字符。。。。。不推薦 "scripting.FileSystemObject"

例子:頁面接受傳入ID參數,在數據庫里查找ID的記錄 寫入內容到HTML文件,先創建一個HTML模板文件如“temp_article.html”的文件,讀入好久替換里面的內容標簽“[-content-]”

<%
dim s,ss,id
id= ""& request.QueryString("id")
if id="" then
response.Write("id para is empty")
response.end
else
WriteFile id
end if
'-----------------------------------------------
Function readFile2(fileName)
dim fso,fileobj,fileContent
Set fso = Server.CreateObject("scripting.FileSystemObject") '創建FSO對象
Set fileObj = fso.opentextfile(fileName,1,true) '創建文件讀取對象,用於字符文件
filecontent = fileObj.readall '用文件讀取對象讀出文件內容
readFile2=filecontent
Set fileObj = nothing
Set fso = nothing
end function

'-------------------------------------------------
'函數名稱:ReadTextFile
'作用:利用AdoDb.Stream對象來讀取UTF-8格式的文本文件
'----------------------------------------------------
Function ReadFromTextFile (FileUrl)
dim str
set stm=server.CreateObject("adodb.stream")
stm.Type=2 '以本模式讀取
stm.mode=3
stm.charset="utf-8"
stm.open
stm.loadfromfile FileUrl
str=stm.readtext
stm.Close
set stm=nothing
ReadFromTextFile=str
End Function
'-------------------------------------------------
'函數名稱:WriteToTextFile
'作用:利用AdoDb.Stream對象來寫入UTF-8格式的文本文件
'----------------------------------------------------
Sub WriteToTextFile (FileUrl,byval Str,CharSet)
set stm=server.CreateObject("adodb.stream")
stm.Type=2 '以本模式讀取
stm.mode=3
stm.charset=CharSet
stm.open
stm.WriteText str
stm.SaveToFile FileUrl,2
stm.flush
stm.Close
set stm=nothing
End Sub

Function ReadFile(fileName )
dim fso,fileobj,filecontent
Set fso = Server.CreateObject("scripting.FileSystemObject") '創建FSO對象
Set fileObj = fso.opentextfile(fileName,1,true) '創建文件讀取對象,用於字符文件
filecontent = fileObj.readall '用文件讀取對象讀出文件內容
ReadFile=filecontent
Set fileObj = nothing
Set fso = nothing

end Function

Function WriteFile(id)

dim fso,fileobj,fileName,content,Sqlpp,tbname
dim str
tbname="yao"
Sqlpp ="select ID,Title,content from "&tbname&"_Article Where ID="& ID
Set Rspp=server.CreateObject("adodb.recordset")
rspp.open sqlpp,conn,1,1
if rspp.recordcount<=0 then
Response.write "no record find "
response.end
rspp.close
set rspp=nothing
end if
Do while not Rspp.Eof
'Response.Write( Rspp("content") & "-
" & VbCrLf)
content=Rspp("content")
Rspp.Movenext
Loop
rspp.close
set rspp=nothing

fileName= server.mappath(".") &"\temp_article.html"
'str= ReadFile(fileName) 
'str =readFile2(fileName)
str =ReadFromTextFile(fileName)

str=   Replace(str,"[-body-]",content)
filename=server.mappath("../html/" ) & id & ".html"
call WriteToTextFile(fileName,str,"utf-8")
response.Write(fileName & "------OK")

exit function
'response.Write("fid = "&fid) '調試使用,輸出請求參數
'response.Write("content = "&content) ’調試使用,輸出表單提交數據
Set fso = Server.CreateObject("scripting.FileSystemObject") '創建FSO對象
Set fileObj = fso.opentextfile(filename,2,true) '使用FSO創建文件寫入對象
fileObj.Write ( "000000" & str )'向文件寫入數據,覆蓋形式寫入
fileObj.close '推送內容寫入並關閉寫入通道
response.Write(fileName & "------OK")
Set fileObj = nothing
Set fso = nothing

end Function

%>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM