zoukankan      html  css  js  c++  java
  • Asp用FSO读写文件

    使用范例需要两个条件:

    1.(读写都需要)服务器开启父目录

    2.(写文件需要)服务器开启写入权限

    (一)读取根目录下的所有文件及文件夹信息并输出

    'filelist.asp

    '作用:读取path目录下所有子目录和文件并以表格输出其信息

    'path为读取文件相对根目录的路径,如根目录是"/"

    <table width="100%">
    <tr><td>名称</td><td>父目录</td><td>属 性</td><td>大小</td><td>日期</td><td> </td><td> </td>
    </tr><%
    path = request.QueryString("path")        '读取路径参数
    if path <> "" then path = path else path = "/" end if '不传参数时默认读根目录
    rootpath = Server.MapPath(path)       '获得path路径所在的服务器完整路径,需要开启父目录
    Set fileObj = Server.CreateObject("Scripting.FileSystemObject") '创建FSO对象
    Set root = fileObj.GetFolder(rootpath) '创建文件夹对象
    For each folder in root.subfolders '读取文件夹对象下的所有子目录并显示其信息
       if path<>"/" then tmppath = path&"/"&folder.name else tmppath = path&folder.name end if
       Response.Write("<tr><td>"&folder.name&"</td><td>"&folder.ParentFolder.name&"< /td><td>目录</td><td>     </td><td>"&folder.DateCreated&"</td>")
       Response.Write("<td><a href=?path="&tmppath&">打开</a></td><td>删除</td></tr>")
    next
    For each fileitem in root.files '读取目录下所有文件并显示其信息
       if path<>"/" then tmppath = path&"/"&fileitem.name else tmppath = path&fileitem.name end if
       'response.Write(tmppath)
       Response.Write("<tr><td>"&fileitem.name&"</td><td>"&fileitem.ParentFolder.name&"< /td><td>文件</td><td>"&fileitem.size&" byte</td><td>"&fileitem.datelastmodified&"</td>")
       Response.Write("<td><a href=editfile.asp?fid="&tmppath&">打开</a></td><td>删除</td></tr>")
    next
    Set fileObj = nothing
    Set root = nothing
    Set folder = nothing
    Set fileitem = nothing
    %></table>

    (二)读取某个文件内容并输出到文本区域

    <%
    fid = Request.QueryString("fid") 'fid为文件相对根目录的路径,如根目录下的index.htm的fid = /index.htm
    %>

    <%
    Set fso = Server.CreateObject("scripting.FileSystemObject") '创建FSO对象
    Set fileObj = fso.opentextfile(server.mappath(fid),1,true) '创建文件读取对象,用于字符文件
    filecontent = fileObj.readall '用文件读取对象读出文件内容
    %>
    <textarea name="content" rows="38" style="99%;"><%=filecontent%></textarea>
    <%
    Set fileObj = nothing
    Set fso = nothing
    %>

    (三)使用FSO写入文件

    <%
    content = Request.Form("content") '表单提交的数据
    fid = request.QueryString("fid") '请求参数,指向文件存取相对根目录的路径
    'response.Write("fid = "&fid) '调试使用,输出请求参数
    'response.Write("content = "&content) ’调试使用,输出表单提交数据
    Set fso = Server.CreateObject("scripting.FileSystemObject") '创建FSO对象
    Set fileObj = fso.opentextfile(server.mappath(fid),2,true) '使用FSO创建文件写入对象
    fileObj.Write content '向文件写入数据,覆盖形式写入
    fileObj.close '推送内容写入并关闭写入通道
    response.Write("保存成功")
    Set fileObj = nothing
    Set fso = nothing
    %>

  • 相关阅读:
    【Silverlight】汉诺塔游戏,带AI
    Farseer Physics Engine
    解决SilverLight的图片裁剪问题
    【C#】三维立体验证码 (3DCaptcha)
    又一个“众所周知”的DAL层设计BUG
    【C#】性别类
    36进制条码序列号生成器 [更新]
    理想的软件设计标准
    表驱动法概念到实战(一) 原理及基本运用
    Sudoku solver
  • 原文地址:https://www.cnblogs.com/lear/p/2120783.html
Copyright © 2011-2022 走看看