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
end Function
%>
总结:用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
%>
相关文章推荐
- Asp.Net 关于FindControl方法使用
- ASP.NET Core MVC Hello World
- ASP.NET 按规定尺寸缩放图片
- 如何在asp.net页面使用css和js
- ASP.NET MVC 5通过viewdata 向视图传递多个对象
- Asp.Net Api2 过滤器的使用
- ASP.NET过滤类SqlFilter,防止SQL注入[原创]_实用技巧_脚本之家
- 中国菜刀Cknife(C刀)自定义模式秒过安全狗. jsp过狗一句话,aspx过狗一句话.
- ASP.NET Web Service如何工作(3)
- ASP.NET Web Service如何工作(2)
- ASP.NET Web Service如何工作(1)
- asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦
- 请求如何进入ASP.NET MVC框架
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET过滤类SqlFilter,防止SQL注入 原创
- 你应该知道的 asp.net webform之异步页面
- 学习asp.net
- ASP.NET中的多线程整理
- 2016-AspNet-MVC教学-6-简单类Student的初次完整创建和使用
- Windows Iot:让Raspberry Pi跑起来(1)