【问题标题】:Pure ASP hyperlink for all files in a folder文件夹中所有文件的纯 ASP 超链接
【发布时间】:2013-04-05 23:15:19
【问题描述】:

我最近有一个项目,在任何地方都找不到简单的代码。希望这对某人有所帮助! 确保为文件夹上的 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
%> 

【问题讨论】:

  • 很好的信息,但如果您发布问题然后自己回答会更好。你可以这样做,没关系,其他人可能有其他答案可以贡献!
  • “file:///”位不是指向您自己的机器而不是服务器上的文件吗?
  • 在我的示例中,他们从公司网络上的共享驱动器中提取文件,所以 =) 但是是的

标签: file list asp-classic hyperlink directory


【解决方案1】:

我找到了您的帖子,它对我非常有用。我对其进行了一些更改,并想将其发布在这里,以防其他人也发现它也有用。由于我在本地设置 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>")
%>

【讨论】:

    【解决方案2】:

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

    本质上,我们遍历给定目录中的所有文件,然后使用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
    %>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      相关资源
      最近更新 更多