首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写网页以显示名称并链接到文件夹中的所有文件

问编写网页以显示名称并链接到文件夹中的所有文件
EN

Stack Overflow用户
提问于 2011-06-12 08:15:56
回答 2查看 4.1K关注 0票数 1

背景故事和目标:我正在编写一个脚本,通过ftp将文件(文档)从服务器A发送到web服务器B。然后在B上,我希望asp.net网页显示所有文件的名称(以某种方式直观地告诉用户哪些文件在哪个文件夹中),并提供指向该文件的链接。

,我的问题是:,使用asp.net和C#显示目录和子目录内容的好方法是什么?它是通过文件结构运行,从上传的根目录开始,还是我应该修改脚本,在文件夹结构上生成和发送一个use文件,然后使用XmlDataSource?如何设置XmlDataSource的数据路径以确保它使用上传的xml文件?

注意:我相信两者都存在一些并发问题。但我认为这是一个单独的堆叠溢出问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-12 12:56:08

使用找到的代码这里,您只能允许登录用户查看文件夹内容。假设您将登录用户存储在会话对象中,下面是转换为C#的代码,以及仅用于登录用户的签入:

代码语言:javascript
复制
string dir = Request.Form("dir");
if (string.IsNullOrEmpty(dir))
    dir = "/";

if (Session["Logged_User"] == null)
{
    Response.Write("Not Authorized");
    Response.End();
}

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(dir));
StringBuilder sb = new StringBuilder();
sb.Append("<ul class=\"jqueryFileTree\" style=\"display: none;\">").Append(Environment.NewLine);
foreach (System.IO.DirectoryInfo di_child in di.GetDirectories())
{
    sb.AppendFormat("\t<li class=\"directory collapsed\"><a href=\"#\" rel=\"{0}\">{1}</a></li>\n",  dir + di_child.Name, di_child.Name);
}

foreach (System.IO.FileInfo fi in di.GetFiles())
{
    string ext = (fi.Extension.Length > 1) ? fi.Extension.Substring(1).ToLower() : "";
    sb.AppendFormat("\t<li class=\"file ext_{0}\"><a href=\"#\" rel=\"{1}\">{2}</a></li>\n", ext, dir + fi.Name, fi.Name);
}
sb.Append("</ul>");
Response.Write(sb.ToString());
票数 1
EN

Stack Overflow用户

发布于 2011-06-12 08:37:23

我认为最简单的方法是使用从上传文件的目录(而不是根目录)开始的文件结构,.You可以将该目录的路径保存在<AppSettings>部分的web.config文件中,然后使用File和Directory类读取其结构。

在web.config中

代码语言:javascript
复制
<appSettings>
    <add key="UploadDirectory" value="~/Upload/"/>
</appSettings>

在代码后面

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string DirectoryName = Request.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadDirectory"]);
        if (Directory.Exists(DirectoryName))
        {
            String[] Files = Directory.GetFiles(DirectoryName);
            myRepeater.DataSource = Files;
            myRepeater.DataBind();
        }
    }

}
protected void myRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        LinkButton FileName = (LinkButton)e.Item.FindControl("FileName");
        String fullName = (String)e.Item.DataItem;
        FileName.Text = fullName.Substring(fullName.LastIndexOf("\\") + 1);
        FileName.CommandArgument=fullName.Substring(fullName.LastIndexOf("\\") + 1);
    }
}
protected void myRepeater_OnItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName=="GOTO")
    {
        Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["UploadDirectory"]+(String)e.CommandArgument);
    }
}

在aspx中

代码语言:javascript
复制
<asp:Repeater ID="myRepeater" OnItemDataBound="myRepeater_OnItemDataBound" runat="server">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
             <asp:LinkButton runat="server" ID="FileName" CommandName="GOTO"></asp:LinkButton></li>
    </ItemTemplate>
    <FooterTemplate>
        </ul></FooterTemplate>
</asp:Repeater>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6320924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档