您的位置:首页 > 编程语言 > ASP

ASP 读写文件FSO,adodb.stream

2016-03-25 15:05 239 查看
例如静态化页面的时候

总结:用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

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: