【问题标题】:Why is the jQuery FileTree showing files when not set to?为什么 jQuery FileTree 在未设置时显示文件?
【发布时间】:2018-02-27 07:30:43
【问题描述】:

我正在使用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 似乎按文档说明工作,但我不知道是不是因为这是默认行为。

如果我想将此小部件配置为只允许用户选择一个文件夹,我做错了什么?

【问题讨论】:

  • 您使用的是什么连接器?给定 asp.net-mvc 标记,我假设您没有使用默认连接器 (jqueryFileTree.php)。 ASP 连接器(来自这里:github.com/jqueryfiletree/jqueryfiletree/tree/master/dist/…)似乎没有引用您正在使用的选项。
  • 我正在通过 ASP.NET MVC Core 使用自定义连接器。我有一个重新调整预期 HTML 的操作方法。
  • 好的,关键是,连接器是需要过滤文件/目录的连接器。如果您查看 PHP 连接器的代码,您会发现它过滤了文件: if( is_dir($postDir . $file) && (!$onlyFiles || $onlyFolders) )... 我什么也没看到在 ASP 连接器中类似。
  • @BumptiousQBangwhistle 我说我使用的是自定义连接器,所以你看不到任何东西。它只是一个简单的 MVC 控制器,带有一个被小部件调用的 roots 动作,以及一个也被调用的“文件”动作。
  • 我明白了。我的观点是,处理您正在使用的选项的是连接器。您的自定义连接器需要识别已发布变量的状态并相应地处理文件/目录。

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


【解决方案1】:

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

注意 - 我们无法深入了解您的文件,因此这是一个使用一些样板代码的新手示例。另外,我知道您的答案是关于 .NET core,但即使 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);
}

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多