<p>使用范例需要两个条件:</p> <p>1.(读写都需要)服务器开启父目录</p> <p>2.(写文件需要)服务器开启写入权限</p> <p>(一)读取根目录下的所有文件及文件夹信息并输出</p> <p>&#39;filelist.asp</p> <p>&#39;作用:读取path目录下所有子目录和文件并以表格输出其信息</p> <p>&#39;path为读取文件相对根目录的路径,如根目录是&quot;/&quot;</p> <p><br /> <br /> &lt;%<br /> path = request.QueryString(&quot;path&quot;) &#39;读取路径参数<br /> if path &lt;&gt; &quot;&quot; then path = path else path = &quot;/&quot; end if &#39;不传参数时默认读根目录<br /> rootpath = Server.MapPath(path) &#39;获得path路径所在的服务器完整路径,需要开启父目录<br /> Set fileObj = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) &#39;创建FSO对象<br /> Set root = fileObj.GetFolder(rootpath) &#39;创建文件夹对象<br /> For each folder in root.subfolders &#39;读取文件夹对象下的所有子目录并显示其信息<br /> if path&lt;&gt;&quot;/&quot; then tmppath = path&amp;&quot;/&quot;&amp;folder.name else tmppath = path&amp;folder.name end if<br /> Response.Write(&quot;&quot;)<br /> Response.Write(&quot;&quot;)<br /> next<br /> For each fileitem in root.files &#39;读取目录下所有文件并显示其信息<br /> if path&lt;&gt;&quot;/&quot; then tmppath = path&amp;&quot;/&quot;&amp;fileitem.name else tmppath = path&amp;fileitem.name end if<br /> &#39;response.Write(tmppath)<br /> Response.Write(&quot;&quot;)<br /> Response.Write(&quot;&quot;)<br /> next<br /> Set fileObj = nothing<br /> Set root = nothing<br /> Set folder = nothing<br /> Set fileitem = nothing<br /> %&gt;</p> <table width="100%"> <tbody> <tr> <td>名称</td> <td>父目录</td> <td>属 性</td> <td>大小</td> <td>日期</td> <td></td> <td></td> </tr> <tr> <td>&quot;&amp;folder.name&amp;&quot;</td> <td>&quot;&amp;folder.ParentFolder.name&amp;&quot;&lt; /td&gt;</td> <td>目录</td> <td> </td> <td>&quot;&amp;folder.DateCreated&amp;&quot;</td> <td><a href="?path=">打开</a></td> <td>删除</td> </tr> <tr> <td>&quot;&amp;fileitem.name&amp;&quot;</td> <td>&quot;&amp;fileitem.ParentFolder.name&amp;&quot;&lt; /td&gt;</td> <td>文件</td> <td>&quot;&amp;fileitem.size&amp;&quot; byte</td> <td>&quot;&amp;fileitem.datelastmodified&amp;&quot;</td> <td><a href="editfile.asp?fid=">打开</a></td> <td>删除</td> </tr> </tbody> </table> <p>(二)读取某个文件内容并输出到文本区域</p> <p>&lt;%<br /> fid = Request.QueryString(&quot;fid&quot;) &#39;fid为文件相对根目录的路径,如根目录下的index.htm的fid = /index.htm<br /> %&gt;</p> <p>&lt;%<br /> Set fso = Server.CreateObject(&quot;scripting.FileSystemObject&quot;) &#39;创建FSO对象<br /> Set fileObj = fso.opentextfile(server.mappath(fid),1,true) &#39;创建文件读取对象,用于字符文件<br /> filecontent = fileObj.readall &#39;用文件读取对象读出文件内容<br /> %&gt;<br /> <textarea name="content" rows="38" >&lt;%=filecontent%&gt;</textarea><br /> &lt;%<br /> Set fileObj = nothing<br /> Set fso = nothing<br /> %&gt;</p> <p>(三)使用FSO写入文件</p> <p>&lt;%<br /> content = Request.Form(&quot;content&quot;) &#39;表单提交的数据<br /> fid = request.QueryString(&quot;fid&quot;) &#39;请求参数,指向文件存取相对根目录的路径<br /> &#39;response.Write(&quot;fid = &quot;&amp;fid) &#39;调试使用,输出请求参数<br /> &#39;response.Write(&quot;content = &quot;&amp;content) &rsquo;调试使用,输出表单提交数据<br /> Set fso = Server.CreateObject(&quot;scripting.FileSystemObject&quot;) &#39;创建FSO对象<br /> Set fileObj = fso.opentextfile(server.mappath(fid),2,true) &#39;使用FSO创建文件写入对象<br /> fileObj.Write content &#39;向文件写入数据,覆盖形式写入<br /> fileObj.close &#39;推送内容写入并关闭写入通道<br /> response.Write(&quot;保存成功&quot;)<br /> Set fileObj = nothing<br /> Set fso = nothing<br /> %&gt;</p>
T:0.006952s,M:252.05 KB
返回顶部 留言