我为我所工作的公司开发了一个ASP安全页面.有一个登陆(登录页面),一旦您通过身份验证,您将进入一个包含多个子页面链接的页面.每个子页面都有一个文件夹结构.例如:有一个会议纪要标题,然后在下面,缩进是引用包含信息的PDF的链接.可能有3或4个标题,文档链接在下面.
原始版本有一个PHP脚本,它运行并将从文件夹结构同步到服务器上的实时站点,该文件夹结构将被模仿到实际站点上.因此,如果我有一个名为Folder1的文件夹和名为test1 test2 test3的子文件夹,则实时网站会相应地显示它们.由于该站点现在是ASP而不是PHP .. PHP脚本不再起作用(因为PHP不能很好地与ASP一起使用).
我发现一个在线片段有点适用于我想要实现的目标(即文件夹/子文件夹/文件名称结构),但是我现在停留在如何链接文件以便在单击时打开它们.我一直在文件名中看到%25.我知道%20与空格相同,因为我处理的是包含空格的文件和文件夹名称,这似乎是我的问题.我尝试添加%20但空格变为"%2520".
如果您查看下面的代码,则会在底部显示一个名为"MapURL"的链接.当我试图找出%25来自何处时,我现在已将该链接注释掉了.任何人都有任何想法如何让链接工作?
这是片段.
dim path
path = "PATH TO THE FOLDER ON THE SERVER"
ListFolderContents(path)
sub ListFolderContents(path)
dim fs, folder, file, item, url
set fs = CreateObject("Scripting.FileSystemObject")
set folder = fs.GetFolder(path)
'Display the target folder and info.
Response.Write("<ul><b>" & folder.Name & "</b>") '- " _
' & folder.Files.Count & " files, ")
'if folder.SubFolders.Count > 0 then
' Response.Write(folder.SubFolders.Count & " directories, ")
'end if
'Response.Write(Round(folder.Size / 1024) & " KB total." _
' & "</ul>" & vbCrLf)
Response.Write("<ul>" & vbCrLf)
'Display a list of sub folders.
for each item in folder.SubFolders
ListFolderContents(item)
next
'Display a list of files.
for each item in folder.Files
'url = MapURL(item.path)
'Response.Write("<li><a href=" & url & ">" & item.Name & "</a> - " _
Response.Write("<li><a href=" & Replace(item.path," ","%") & ">" & item.Name & "</a> - " _
& item.Name & "</a>" _
& "</li>" & vbCrLf)
next
Response.Write("</ul>" & vbCrLf)
Response.Write("</ul>" & vbCrLf)
end sub
function MapURL(path)
dim rootPath, url
'Convert a physical file path to a URL for hypertext links.
rootPath = Server.MapPath("/")
url = Right(path, Len(path) - Len(rootPath))
MapURL = Replace(url, "\", "/")
end function
Run Code Online (Sandbox Code Playgroud)
你的代码有几个问题.
FileSystemObject每次调用递归ListFolderContents()函数时都会创建一个new .这不必要地浪费,并且一旦输出多个文件就会变慢.Folder对象作为第一个参数,而不是路径.这使事情变得容易多了.<b>不能合法地成为孩子<ul>.我完全重写了你的代码,以产生更正确的输出并尽可能快.问题的关键是PathEncode()函数,它将相对路径转换为正确编码的URL.其他事情应该是不言自明的:
ListFolder "P:\ATH\TO\THE\FOLDER\ON\THE\SERVER"
' -- Main Functions ----------------------------------------------------
Sub ListFolder(path)
Dim fs, rootPath
Set fs = CreateObject("Scripting.FileSystemObject")
rootPath = Replace(path, Server.MapPath("/"), "") & "\"
ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub
' ----------------------------------------------------------------------
Sub ListFolderContents(folder, relativePath)
Dim child
Say "<ul>"
Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"
For Each child In folder.SubFolders
If Not IsHidden(child) Then
ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
End If
Next
relativePath = h(relativePath)
For Each child In folder.Files
If Not IsHidden(child) Then
Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
End If
Next
Say "</ul>"
End Sub
' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
Response.Write s & vbNewLine
End Sub
Function h(s)
h = Server.HTMLEncode(s)
End Function
Function PathEncode(s)
' this creates a more correct variant of what Server.URLEncode would do
PathEncode = Replace(s, "\", "/")
PathEncode = Server.URLEncode(PathEncode)
PathEncode = Replace(PathEncode, "+", "%20")
PathEncode = Replace(PathEncode, "%2F", "/")
PathEncode = Replace(PathEncode, "%2E", ".")
PathEncode = Replace(PathEncode, "%5F", "_")
End Function
Function IsHidden(File)
IsHidden = File.Attributes And 2 = 2
End Function
Run Code Online (Sandbox Code Playgroud)
笔记
<div class="folder">将CSS样式(即粗体等)应用于文件夹名称.relativePath参数用于使工作负载尽可能低 - 当文件夹有1000个文件时,计算整个相对路径1000次是没有意义的.借助此参数,仅处理实际更改的部分.Say()或h()周围的功能可以节省大量的打字,并使代码更加干净.您可能需要在 href ("") 处添加额外的引号。最好的方法是查看生成的源代码(从结果页面),基本上<a href=""" & replace(...) & """>"
,如果您只使用一个引号,它只会关闭字符串,但您会缺少 href= 和结束引号之后所需的 HTML 引号。
| 归档时间: |
|
| 查看次数: |
12698 次 |
| 最近记录: |