Friday, September 29, 2017

Why is the jQuery FileTree showing files when not set to?

Leave a Comment

I'm using the jqueryfiletree plugin, and it seems established and fairly smooth, but I have some issues:

Despite the options being set as follows:

$(function() {     $("#sourceFileTree").fileTree({         onlyFolders: true,         root: "C%3a%5cProjects%5cBMW%5cCode%5cFileTransfers.Web",         script: "/FileTree/Tree",         multiFolder: false,         multiSelect: false,         preventLinkAction: true     }); }); 
  1. onlyFolders seems to be ignored, and any folder opened also shows files it contains.
  2. The same goes for multiSelect: false. While I can "select" (highlight it in bold) only one file at a time, I can still check as many folder and file checkboxes as I want.
  3. Only multiFolder: false seems to work as documented, but I don't know if it's because that's a default behaviour anyway.

What am I doing wrong if I want to configure this widget to allow the user to select only one folder?

1 Answers

Answers 1

The connector (yes, yours is custom) is what performs the filtering on the results. If you are not looking for / using the passed parameters from the jQuery plugin, then the results will not be what you expect. From the link someone posted above (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), we can update this just a bit to return a better result set.

Note - we do not have insight into your files, so this is a very freshman example using some boilerplate code. Also, I know your answer is about .NET core, but the logic should still hold true even if the syntax is not exactly the same between 4.6 and 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); } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment