Jquery EasyUI tree 的异步加载(遍历指定文件夹,根据文件夹内的文件生成tree)具体代码如下:
sqlConnection82 = System.Configuration.ConfigurationManager.AppSettings["LocalConnectionString"];
string path = context.Server.MapPath(@"~/CISWeb/SMT_SOP");
string id = string.Empty;
List filesNameList = getFiles(path);
if (filesNameList.Count > 0)
context.Response.Write(ListToJson(filesNameList,id));
else
{
context.Response.Write("0");
}
}
///
/// 范围指定目录下的 文件夹/文件 数量
///
///
///
private static List getFiles(string path)
{
DirectoryInfo SMT_SOP = new DirectoryInfo(path);
List allFileList = new List();
DirectoryInfo[] allDir = SMT_SOP.GetDirectories();//获取文件夹数量
foreach (DirectoryInfo d in allDir)
{
allFileList.Add("0|" + d.Name);
}
FileInfo[] allFile = SMT_SOP.GetFiles();//获取文件数量
foreach (FileInfo fi in allFile)
{
allFileList.Add("1|" + fi.Name);
}
return allFileList;
}
///
/// 遍历指定文件夹,根据文件夹内的文件返回JSON
///
///
/// 调用这个方法的时候,id=""
///
public string ListToJson(List filesNameList,string id)
{
StringBuilder sb2 = new StringBuilder();
sb2.Append("[");
for (int i = 0; i < filesNameList.Count; i++)
{
if (filesNameList[i].ToString().Split('|')[0] == "0")
{
sb2.Append("{ \"id\":" +id+ (i + 1).ToString() + ",\"text\":\"" + filesNameList[i].ToString().Split('|')[1] + "\",\"state\":\"closed\",\"children\": [");
sb2.Append("]},");
}
else
{
sb2.Append("{\"id\":"+id + (i + 1).ToString() + ",\"text\":\"" + String.Format(filesNameList[i].ToString().Split('|')[1]) + "\"},");
}
}
sb2.Remove(sb2.Length - 1,1);
sb2.Append("]");
return sb2.ToString();
}