我为我所工作的公司开发了一个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
答案 0 :(得分:5)
您的代码存在一些问题。
FileSystemObject
函数时,都会创建一个新的ListFolderContents()
。这是不必要的浪费,一旦输出的文件数量超过少数就会变慢。Folder
对象作为第一个参数,而不是路径。这使事情变得容易多了。<b>
<ul>
无法合法成为PathEncode()
的孩子。我完全重写了您的代码以产生更正确的输出并尽可能快。问题的关键是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
函数,它将相对路径转换为正确编码的URL。其他事情应该是不言自明的:
<div class="folder">
注释
relativePath
将CSS样式(即粗体等)应用于文件夹名称。Say()
参数用于保持工作负载尽可能低 - 当文件夹有1000个文件时,计算整个相对路径1000次是没有意义的。借助此参数,仅处理实际更改的部分。h()
或{{1}}这样的功能可以为您节省大量的打字工作,同时也让代码更加干净。答案 1 :(得分:2)
你可能需要href(“”)的额外引号。最好的方法是查看生成的源代码(来自结果页面),如<a href=""" & replace(...) & """>"
基本上,如果你只使用一个引用它只是关闭字符串,但你缺少href =和结束之后所需的HTML引用。