编码网页显示名称和链接到所有文件夹中 [英] Coding web page to display name and link to all files in a folder

查看:90
本文介绍了编码网页显示名称和链接到所有文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的故事背景与目标:我工作的一个脚本通过FTP发送从服务器A的文件(文件)到web服务器B.然后在BI希望asp.net网页present所有文件的文件名(以某种方式直观地告知哪些文件是什么文件夹中的用户),并提供一个链接到该文件。

我的问题是:什么是显示目录和子目录的使用asp.net和C#网站内容的好方法吗?将它的工作只是去通过文件结构上传根目录开始,或者我应该修改脚本生成,并通过文件夹结构发送XMLFILE,然后使用的XmlDataSource?我将如何设置数据路径的的XmlDataSource以确保它会使用上传的xml文件?


  

注:的我相信有既一些并发问题。但我相信这是一个单独的计算器问题。



解决方案

使用code发现的这里您可以在用户查看该文件夹的内容只允许登录。假设你存储登录的用户在一个Session对象,这里是code转换为C#加上检查登录用户只:

 字符串DIR =的Request.Form(目录);
如果(string.IsNullOrEmpty(DIR))
    DIR =/;如果(会话[Logged_User] == NULL)
{
    的Response.Write(不授权);
    到Response.End();
}System.IO.DirectoryInfo DI =新System.IO.DirectoryInfo(使用Server.Mappath(DIR));
StringBuilder的SB =新的StringBuilder();
sb.Append(< UL类= \\jqueryFileTree \\的风格= \\显示:无; \\>中)。追加(Environment.NewLine);
的foreach(System.IO.DirectoryInfo di_child在di.GetDirectories())
{
    sb.AppendFormat(\\ T&下;利类= \\目录折叠\\>&下; A HREF = \\#\\相对= \\{0} \\> {1}&下; / A>&下; /立GT; \\ N,DIR + di_child.Name,di_child.Name);
}的foreach(System.IO.FileInfo网络di.GetFiles())
{
    串EXT =(fi.Extension.Length→1)? fi.Extension.Substring(1).ToLower():,;
    sb.AppendFormat(\\ T&下;利类= \\文件EXT_ {0} \\>&下; A HREF = \\#\\相对= \\{1} \\> {2}&下; / A&GT ;< /李> \\ N,EXT,DIR + fi.Name,fi.Name);
}
sb.Append(&下; / UL>中);
的Response.Write(sb.ToString());

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

解决方案

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

这篇关于编码网页显示名称和链接到所有文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆