开发者

Coding web page to display name and link to all files in a folder

开发者 https://www.devze.com 2023-03-12 13:12 出处:网络
Background story and goal: I\'m working on a script to send files (documents) from server A via ftp to web server B. Then on B I want the asp.net web page to 开发者_JS百科present the name of all the f

Background story and goal: I'm working on a script to send files (documents) from server A via ftp to web server B. Then on B I want the asp.net web page to 开发者_JS百科present the name of all the files (somehow visually informing the user of what files are in what folder) and provide a link to that file.

My question is: what is a good way to display the content of directories and sub directories by a website using asp.net and C#? Would it work just go through the file structure starting in the uploaded root directory or should I modify the script to generate and send a xmlfile over the folder structure and then use the XmlDataSource? How would I set the data path for the XmlDataSource to ensure that it would use the uploaded xml file?

Note: I believe there are some concurrency issues with both. But I believe that's a separate stackoverflow question.


I think the simplest way is using file structure starting from the directory where you uploaded your files(not from root directory).You can save the path of that directory in your web.config file in <AppSettings> section, then read its structure using File and Directory classes.

in web.config

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

In code behind

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);
    }
}

in aspx

<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>


Using the code found here you can allow only logged in users to view the folder contents. Assuming you store the logged in user in a Session object, here is the code translated to C# plus the check for logged in user only:

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号