为什么 jQuery FileTree 在未设置时显示文件?
Why is the jQuery FileTree showing files when not set to?
我正在使用 jqueryfiletree plugin,它似乎已经建立并且相当流畅,但我有一些问题:
尽管选项设置如下:
$(function() {
$("#sourceFileTree").fileTree({
onlyFolders: true,
root: "C%3a%5cProjects%5cBMW%5cCode%5cFileTransfers.Web",
script: "/FileTree/Tree",
multiFolder: false,
multiSelect: false,
preventLinkAction: true
});
});
onlyFolders
似乎被忽略了,任何打开的文件夹也会显示它包含的文件。
multiSelect: false
也是如此。虽然我一次只能 "select"(以粗体突出显示)一个文件,但我仍然可以根据需要选中任意多个文件夹和文件复选框。
- 只有
multiFolder: false
似乎按照记录工作,但我不知道这是不是因为这是默认行为。
如果我想配置此小部件以允许用户 select 只有一个文件夹,我做错了什么?
连接器(是的,您的是自定义的)用于对结果执行过滤。如果您没有寻找/使用来自 jQuery 插件的传递参数,那么结果将不是您所期望的。从上面发布的 link 某人 (https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/Asp.Net-MVC/FileTreeController.cs) and the PHP version which seems to use the applicable options (https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/jqueryFileTree.php),我们可以稍微更新一下以 return 一个更好的结果集。
注意 - 我们无法深入了解您的文件,因此这是一个非常新手的示例,使用了一些样板代码。另外,我知道你的答案是关于 .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);
}
我正在使用 jqueryfiletree plugin,它似乎已经建立并且相当流畅,但我有一些问题:
尽管选项设置如下:
$(function() {
$("#sourceFileTree").fileTree({
onlyFolders: true,
root: "C%3a%5cProjects%5cBMW%5cCode%5cFileTransfers.Web",
script: "/FileTree/Tree",
multiFolder: false,
multiSelect: false,
preventLinkAction: true
});
});
onlyFolders
似乎被忽略了,任何打开的文件夹也会显示它包含的文件。multiSelect: false
也是如此。虽然我一次只能 "select"(以粗体突出显示)一个文件,但我仍然可以根据需要选中任意多个文件夹和文件复选框。- 只有
multiFolder: false
似乎按照记录工作,但我不知道这是不是因为这是默认行为。
如果我想配置此小部件以允许用户 select 只有一个文件夹,我做错了什么?
连接器(是的,您的是自定义的)用于对结果执行过滤。如果您没有寻找/使用来自 jQuery 插件的传递参数,那么结果将不是您所期望的。从上面发布的 link 某人 (https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/Asp.Net-MVC/FileTreeController.cs) and the PHP version which seems to use the applicable options (https://github.com/jqueryfiletree/jqueryfiletree/blob/master/dist/connectors/jqueryFileTree.php),我们可以稍微更新一下以 return 一个更好的结果集。
注意 - 我们无法深入了解您的文件,因此这是一个非常新手的示例,使用了一些样板代码。另外,我知道你的答案是关于 .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);
}