为什么jQuery FileTree在未设置时显示文件?

时间:2017-09-18 17:06:16

标签: jquery asp.net-mvc jquery-plugins

我正在使用jqueryfiletree plugin,它似乎已经建立并相当顺利,但我有一些问题:

尽管选项设置如下:

$(function() {
    $("#sourceFileTree").fileTree({
        onlyFolders: true,
        root: "C%3a%5cProjects%5cBMW%5cCode%5cFileTransfers.Web",
        script: "/FileTree/Tree",
        multiFolder: false,
        multiSelect: false,
        preventLinkAction: true
    });
});
  1. onlyFolders似乎被忽略了,任何打开的文件夹也会显示它包含的文件。
  2. multiSelect: false同样如此。虽然我可以一次只“选择”(以粗体突出显示)一个文件,但我仍然可以根据需要检查多个文件夹和文件复选框。
  3. 只有multiFolder: false似乎有记录,但我不知道是否因为这是默认行为。
  4. 如果我想配置此小部件以允许用户只选择一个文件夹,我做错了什么?

1 个答案:

答案 0 :(得分:4)

连接器(是的,你的是自定义的)是对结果执行过滤的原因。如果您不是在寻找/使用jQuery插件中传递的参数,那么结果将不是您所期望的。从上面发布的链接(https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/Asp.Net-MVC/FileTreeController.cs)和似乎使用适用选项(https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/jqueryFileTree.php)的PHP版本中,我们可以稍微更新一下以返回更好的结果集。

注意 - 我们无法深入了解您的文件,因此这是一个使用一些样板代码的非常新手的例子。另外,我知道你的答案是关于.NET核心的,但即使4.6和Core之间的语法不完全相同,逻辑应该仍然适用

[HttpPost]
//notice the added additional params to the expected request variables
//these appear to match the names of the jQuery options
public virtual ActionResult GetFiles(string dir, bool multiSelect, 
    bool onlyFolders, bool onlyFiles)
{
    const string baseDir = @"/App_Data/userfiles/";

    dir = Server.UrlDecode(dir);
    string realDir = Server.MapPath(baseDir + dir);

    //validate to not go above basedir
    if (! realDir.StartsWith(Server.MapPath(baseDir)))
    {
        realDir = Server.MapPath(baseDir);
        dir = "/";
    }

    List<FileTreeViewModel> files = new List<FileTreeViewModel>();

    DirectoryInfo di = new DirectoryInfo(realDir);

    foreach (DirectoryInfo dc in di.GetDirectories())
    {                
        files.Add(new FileTreeViewModel() { Name = dc.Name, Path = String.Format("{0}{1}\\", dir, dc.Name), IsDirectory = true });
    }

    foreach (FileInfo fi in di.GetFiles())
    {
        files.Add(new FileTreeViewModel() { Name = fi.Name, Ext = fi.Extension.Substring(1).ToLower(), Path = dir+fi.Name, IsDirectory = false });
    }
    //lets filter some results using the properties of 
    //the `FileTreeViewModel()` class
    //I have no idea how you are wanting to use multiSelect, so 
    //it has been left out of this example.
    if(onlyFolders){
        files = files.Where(x=>x.IsDirectory).ToList();
    }
    if(onlyFiles){
        files = files.Where(x=>!x.IsDirectory).ToList();
    }
    return PartialView(files);
}