我是用VB.NET写的,比较简单,7-s2是目录浏览的文件代码,7-s3是查看文件内容(只加了word文档/excel/jpg图片)的文件代码,
以下是7-s2.aspx的代码,其中e:\document是要浏览的文件目录:
Imports System.IO
Public Class _7_s2 Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 Dim dirobj As DirectoryInfo If Request.QueryString.Get("dir") = "" Then dirobj = New DirectoryInfo("e:\document") If dirobj.Exists = False Then Response.Write("该文件夹不存在") Else dispinfo("e:\document") End If Else dispinfo(HttpUtility.UrlDecode(Request.QueryString.Get("dir"))) End If
End Sub Private Sub dispinfo(ByVal pathstr As String) Dim dirobj As DirectoryInfo Dim arrstr() As DirectoryInfo Dim arrstr1() As FileInfo Dim i As Integer dirobj = New DirectoryInfo(pathstr) Response.Write("<center><h2>搜索指定文件夹中的全部内容<hr></h2></center>") Response.Write("您现在浏览的目录是:" & dirobj.FullName.ToString) Response.Write("<br>该目录创建时间为:" & dirobj.CreationTime.ToString) Response.Write("<br>最后修改时间为:" & dirobj.LastWriteTime.ToString & "<br>")
arrstr = dirobj.GetDirectories If arrstr.Length > 0 Then Response.Write("该目录下的子目录有:<br>") Response.Write("<table border='2' width='90%'>") For i = 0 To arrstr.Length - 1 Response.Write("<tr><td><a href='7-s2.aspx?dir=" & HttpUtility.UrlEncode(arrstr(i).FullName) & "'>" & arrstr(i).Name & "</a></td></tr>") Next Response.Write("</table>") End If
arrstr1 = dirobj.GetFiles
If arrstr1.Length > 0 Then Response.Write("该目录下的文件有:<br>") Response.Write("<table border='2' width='90%'>") For i = 0 To arrstr1.Length - 1 Response.Write("<tr><td><a href='7-s3.aspx?file=" & HttpUtility.UrlEncode(arrstr1(i).FullName) & "'>" & arrstr1(i).Name & "</a></td></tr>") Next Response.Write("</table>") End If End Sub End Class
7-s3.aspx文件的代码为:
Imports System.IO Public Class _7_s3 Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 Dim fileobj As FileInfo fileobj = New FileInfo(HttpUtility.UrlDecode(Request.QueryString.Get("file"))) Select Case LCase(fileobj.Extension) Case ".doc" Response.ContentType = "Application/msword" Case ".xls" Response.ContentType = "Application/msexcel" Case ".jpg" Response.ContentType = "image/JPEG" End Select
Response.WriteFile(HttpUtility.UrlDecode(Request.QueryString.Get("file"))) End Sub
End Class