ASP.NET 文件浏览器路径转换

发布于 2024-11-16 09:20:45 字数 1513 浏览 6 评论 0原文

我正在开发一个 asp.net 文件浏览器,它递归地遍历文件夹并列出它们的文件和子文件夹。不过,我也想让文件可以下载/查看,但我似乎遇到了问题。我无法正确翻译地址。我有以下配置和代码。

编辑

问题是创建的链接

Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");

没有正确链接到文件。还有一个我需要解决的额外问题:某些文件名包含空格。

Web.config

<appSettings>
  <add key="UploadDirectory" value="~/Upload/"/>
</appSettings>

FileBrowser.aspx.cs

public partial class FileBrowser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        DirectoryInfo di = new DirectoryInfo(Request.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadDirectory"]));
        if (Directory.Exists(di.ToString()))
            printDir("", di.ToString());

    }

    protected void printDir(string space, string dir)
    {
        DirectoryInfo di = new DirectoryInfo(dir);
        foreach (DirectoryInfo d in di.GetDirectories())
        {
            Response.Write(space + "<a href=" + d.ToString() + ">" + d.ToString() + "</a><br/>");
            printDir(space + "&nbsp;&nbsp;&nbsp;&nbsp;", dir + "\\" + d.ToString());
        }

        foreach (FileInfo d in di.GetFiles())
        {
            Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");
        }
    }
}

I'm working on an asp.net file browser that recursively goes through folder and lists their files and subfolders. However I also want to make the files possible to download/view and it’s there I seem to have a problem. I can’t get the address translation correct. I have the following configuration and code.

Edit:

The problem is that the links that are created

Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");

Do not link to the file correctly. Also there is a bonus problem that I need to resolve: some of the filenames contain spaces.

Web.config

<appSettings>
  <add key="UploadDirectory" value="~/Upload/"/>
</appSettings>

FileBrowser.aspx.cs

public partial class FileBrowser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        DirectoryInfo di = new DirectoryInfo(Request.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadDirectory"]));
        if (Directory.Exists(di.ToString()))
            printDir("", di.ToString());

    }

    protected void printDir(string space, string dir)
    {
        DirectoryInfo di = new DirectoryInfo(dir);
        foreach (DirectoryInfo d in di.GetDirectories())
        {
            Response.Write(space + "<a href=" + d.ToString() + ">" + d.ToString() + "</a><br/>");
            printDir(space + "    ", dir + "\\" + d.ToString());
        }

        foreach (FileInfo d in di.GetFiles())
        {
            Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不奢求什么 2024-11-23 09:20:45

问题是,在渲染子文件夹中文件的路径时,您使用的是“上传/文件名”之类的路径;这是不正确的。

事实上,您应该尝试从当前 di 变量获取目录名称。也就是说,如果您当前正在浏览“Upload”文件夹内的“Inner”文件夹,则您的路径将类似于“Upload/Inner/filename”。

这是您需要进行更改的地方:

  Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");

在上面的代码行中,您需要根据 di 变量中的路径动态创建 href URL。执行以下操作:

  1. 获取 di 路径
  2. 获取 di 路径中“\Upload”后面的子字符串
  3. 将上面的子字符串用“\”分割;这将为您提供任何子目录。
  4. 使用上述子目录创建文件的新路径。

我希望这有帮助。

The problem is that when rendering the path for the files in the subfolders, you're using the path like "Upload/filename"; this is not correct.

In fact, you should try to get the directory names from the current di variable. That is, if you're currently browsing "Inner" folder inside the "Upload" folder, your path would be something like "Upload/Inner/filename".

This is where you need to make changes:

  Response.Write(space + "<a href=" + "Upload/" + d.Name + ">" + d.FullName + "</a><br/>");

In the above code line, you need to create the href URL dynamically depending upon the path in di variable. Do the following:

  1. Get di path
  2. Get the substring after "\Upload" in the di path
  3. Split the above substring by "\"; this will give you any subdirectoryies.
  4. Make a new path to the file using above subdirectories.

I hope this helps.

暮光沉寂 2024-11-23 09:20:45

你尝试过这样的事情吗?

foreach (FileInfo d in di.GetFiles())
{
    Response.Write(space + "<a href=" + Server.MapPath("~/Upload/") + d.Name + ">" + d.FullName + "</a><br/>");
}

Have you tried something like this?

foreach (FileInfo d in di.GetFiles())
{
    Response.Write(space + "<a href=" + Server.MapPath("~/Upload/") + d.Name + ">" + d.FullName + "</a><br/>");
}
深陷 2024-11-23 09:20:45

这更具可读性:

foreach (FileInfo d in di.GetFiles("*", SearchOption.AllDirectories)) // includes subfolders
{
    Response.Write(String.Format("{0}<a href=\"{1}\">{2}</a>", space, Server.MapPath("~/Upload/") + d.Name, d.FullName);
}

也可以尝试 DirectoryInfo。枚举文件()

This is much more readable:

foreach (FileInfo d in di.GetFiles("*", SearchOption.AllDirectories)) // includes subfolders
{
    Response.Write(String.Format("{0}<a href=\"{1}\">{2}</a>", space, Server.MapPath("~/Upload/") + d.Name, d.FullName);
}

Also try DirectoryInfo.EnumerateFile()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文