asp文件管理器代码
时间: 2023-08-06 07:14:50 浏览: 46
ASP 文件管理器可以帮助你在 web 应用程序中管理文件和目录。下面是一个简单的 ASP 文件管理器代码示例:
```asp
<%@ Language=VBScript %>
<% Option Explicit %>
<%
'定义变量
Dim objFSO, objFolder, objFile, objShell
Dim folderPath, filePath, fileExt, fileSize, fileDate
Dim folderCount, fileCount
Dim strAction, strFolderName, strFileName
'初始化变量
folderPath = Request.QueryString("path")
strAction = Request.QueryString("action")
strFolderName = Request.QueryString("folder")
strFileName = Request.QueryString("file")
'创建文件系统对象
Set objFSO = CreateObject("Scripting.FileSystemObject")
'检查目录是否存在
If Not objFSO.FolderExists(folderPath) Then
Response.Write("指定目录不存在!")
Response.End()
End If
'获取目录信息
Set objFolder = objFSO.GetFolder(folderPath)
'获取文件和子目录数量
fileCount = objFolder.Files.Count
folderCount = objFolder.SubFolders.Count
'判断用户操作
Select Case strAction
Case "createFolder"
'创建新的文件夹
If strFolderName <> "" Then
objFSO.CreateFolder folderPath & "\" & strFolderName
End If
Case "deleteFolder"
'删除文件夹
If strFolderName <> "" Then
objFSO.DeleteFolder folderPath & "\" & strFolderName
End If
Case "deleteFile"
'删除文件
If strFileName <> "" Then
objFSO.DeleteFile folderPath & "\" & strFileName
End If
Case "downloadFile"
'下载文件
If strFileName <> "" Then
filePath = folderPath & "\" & strFileName
Set objShell = CreateObject("WScript.Shell")
objShell.Run filePath
End If
End Select
%>
<html>
<head>
<title>ASP 文件管理器</title>
</head>
<body>
<h2>当前目录: <%= folderPath %></h2>
<hr>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="uploadFile">
<input type="hidden" name="path" value="<%= folderPath %>">
<label for="file">上传文件:</label>
<input type="file" name="file" id="file">
<input type="submit" value="上传">
</form>
<hr>
<form method="get">
<input type="hidden" name="path" value="<%= folderPath %>">
<input type="hidden" name="action" value="createFolder">
<label for="folder">创建新文件夹:</label>
<input type="text" name="folder" id="folder">
<input type="submit" value="创建">
</form>
<hr>
<table border="1">
<tr>
<th>名称</th>
<th>类型</th>
<th>大小</th>
<th>修改日期</th>
<th>操作</th>
</tr>
<% '列出所有子文件夹
For Each objFolder In objFolder.SubFolders %>
<tr>
<td><a href="?path=<%= objFolder.Path %>"><%= objFolder.Name %></a></td>
<td>文件夹</td>
<td></td>
<td><%= objFolder.DateLastModified %></td>
<td>
<a href="?path=<%= folderPath %>&action=deleteFolder&folder=<%= objFolder.Name %>" onclick="return confirm('确定删除该文件夹吗?')">删除</a>
</td>
</tr>
<% Next %>
<% '列出所有文件
For Each objFile In objFolder.Files %>
<% '获取文件信息
filePath = objFile.Path
fileExt = objFSO.GetExtensionName(filePath)
fileSize = objFile.Size / 1024 '转换为 KB
fileDate = objFile.DateLastModified %>
<tr>
<td><%= objFile.Name %></td>
<td><%= fileExt %></td>
<td><%= FormatNumber(fileSize, 2) %> KB</td>
<td><%= fileDate %></td>
<td>
<a href="?path=<%= folderPath %>&action=downloadFile&file=<%= objFile.Name %>">下载</a>
<a href="?path=<%= folderPath %>&action=deleteFile&file=<%= objFile.Name %>" onclick="return confirm('确定删除该文件吗?')">删除</a>
</td>
</tr>
<% Next %>
</table>
<hr>
<p>共有 <%= folderCount %> 个子目录和 <%= fileCount %> 个文件。</p>
</body>
</html>
```
上面的代码实现了以下功能:
- 列出指定目录下的所有子文件夹和文件。
- 支持上传文件和创建新的文件夹。
- 支持删除文件和文件夹。
- 支持下载文件。
请注意,这只是个简单的示例,你可以根据自己的需要进行修改和扩展。