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
    %>

  • 相关阅读:
    MVC4学习-View(0)
    javascript与常用正则表达式
    uhfreader&rfid标签测试
    WebClient文件上传很方便哈
    NAudio的简单用法
    与wmi交互,调非托管代码,单元测试遇到的一些问题
    我在这里骑美团单车被交警罚了50元,这个地方不能骑共享单车大家留意了
    发邮件,美化table格式
    学习jwt的简单使用
    学习redis的基本使用
  • 原文地址:https://www.cnblogs.com/lear/p/2120783.html
Copyright © 2011-2022 走看看