文件夹中所有文件的纯ASP超链接

时间:2013-04-05 23:15:20

标签: file list asp-classic hyperlink directory

我最近有一个项目,无法在任何地方找到简单的代码..希望这有助于某人! 确保为该文件夹上的IIS应用程序帐户设置NTFS权限。使用file://表示文件http://表示直接,其他表示相对链接。

<%
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("C:\Path")

for each x in fo.files
Response.Write("<a href=file:///" &x & ">" & x & "</a>" & "</br>")
next

set fo=nothing
set fs=nothing
%> 

2 个答案:

答案 0 :(得分:0)

我找到了你的帖子,这对我来说非常有用。我稍微改了一下,想把它贴在这里,以防其他人发现它也有用。由于我在本地设置IIS以及如何在服务器上设置IIS的方式不同,我必须在上传到服务器而不是本地运行时取消/注释某些行。请记住,我是前端人员,这是我第一次写这样的代码所以请提供反馈:)

<%
    'Dim previewURL As String = "http://XXXXXX/"'Preview
    Dim previewURL As String = ""'Local

    'Dim site As String = "XXXXX"'Preview
    Dim site As String = ""'Local

    Dim currentDir As String = HttpContext.Current.Request.PhysicalApplicationPath.ToString()+site+"\web"

    Dim di As New IO.DirectoryInfo(currentDir)

    Dim diar1 As IO.FileInfo() = di.GetFiles("*.html")', IO.SearchOption.AllDirectories)'change this line if you want sub directories as well

    Dim dra As IO.FileInfo

    Response.Write("<h1>HTML Pages</h1>")
    Response.Write("<ul>")

    'list the names of all files in the specified directory
    For Each dra In diar1
        Response.Write("<li><a href="+previewURL+site+"/web/"+dra.Name+" target=_blank>"+dra.Name+"</a></li>")
    Next

    Response.Write("</ul>")
%>

答案 1 :(得分:0)

为此提供另一种解决方案。
对于某些人可能会绑定到其文件的显式位置但又不想处理网站链接不正确的情况。

本质上,我们遍历给定目录中的所有文件,然后使用GetFileName()切掉整个目录。然后,我们使用/中的根href指向我们想要的文件。然后,用户可以从那里下载所需的内容。

<%
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("C:\Folder0\Folder1\Folder2\main\reports\archive")

for each x in fo.files
        x = fs.GetFileName(x)
        Response.Write("<a href=/main/reports/archive/" & x & " target=_blank>" & x & "</a>" & "</br></br>")
next

set fo=nothing
set fs=nothing
%>