zoukankan      html  css  js  c++  java
  • 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(".") &"	emp_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

    %>

  • 相关阅读:
    VS Code 调试 Angular 和 TypeScript 的配置
    如何在 ASP.NET Core 中发送邮件
    十二个 ASP.NET Core 例子
    ASP.NET Core MVC 源码学习:详解 Action 的激活
    10 分钟学会Linux常用 bash命令
    ASP.NET Core MVC 源码学习:详解 Action 的匹配
    ASP.NET Core MVC 源码学习:MVC 启动流程详解
    ASP.NET Core MVC 源码学习:Routing 路由
    多叉树查找
    《转》Ubuntu 12.04常用的快捷键
  • 原文地址:https://www.cnblogs.com/wgscd/p/5319557.html
Copyright © 2011-2022 走看看