public ActionResult Search(string FolderPath, string SearchQuery = "*") { if (String.IsNullOrWhiteSpace(FolderPath)) { FolderPath = ""; } FolderPath = CleanFolderPath(FolderPath); // A combination of our server path and the specific folder we are looking in for files string ActualPath = Path.Combine(ActualRootPath, FolderPath); //This means that the path that was specified does not exist so I have to take you somewhere, and that would be the root of the file library. if (!Directory.Exists(ActualPath)) { return RedirectToAction("Index"); } // Add each directory and file to the list so we can work with it var libraryItems = new List<FileLibraryItem>(); // Uses the combination defined above as our working directory DirectoryInfo APDI = new DirectoryInfo(ActualPath); // Drop the search query to lower case since we do the same thing to file and folder names SearchQuery = SearchQuery.ToLower(); libraryItems.AddRange(APDI.GetDirectories("*", SearchOption.AllDirectories) .Where(d => d.CreationTime.ToString().ToLower().Contains(SearchQuery) || String.Format("{0:f}", d.CreationTime).ToString().ToLower().Contains(SearchQuery) || d.Name.ToLower().Contains(SearchQuery) ) .Select(directory => new FileLibraryItem() { Name = directory.Name, Path = directory.FullName.Replace(ActualRootPath, "").Replace("\\", "/"), IsDirectory = true, DateAdded = directory.CreationTime })); libraryItems.AddRange(APDI.GetFiles("*", SearchOption.AllDirectories) .Where(f => f.CreationTime.ToString().ToLower().Contains(SearchQuery) || String.Format("{0:f}", f.CreationTime).ToString().ToLower().Contains(SearchQuery) || MimeMapping.GetMimeMapping(f.Name).ToString().ToLower().Contains(SearchQuery) || f.Length.ToString().ToLower().Contains(SearchQuery) || f.Name.ToLower().Contains(SearchQuery) ) .Select(file => new FileLibraryItem() { Name = file.Name, Size = file.Length, Path = RootPath + FolderPath.Replace("\\", "/") + "/" + file.Name, FileType = MimeMapping.GetMimeMapping(file.FullName), DateAdded = file.LastWriteTime })); // Generate the breadcrumbs menu at the top of the page List<Breadcrumb> breadcrumbNavigation = new List<Breadcrumb>(); string[] folders = FolderPath.Split('\\'); for (int i = 0; i < folders.Length; i++) { var breadcrumbEntry = new Breadcrumb() { Name = folders[i], Path = String.Empty }; for (int r = 0; r <= i; r++) { if (r != 0) { breadcrumbEntry.Path += "/"; } breadcrumbEntry.Path += folders[r]; } if (!String.IsNullOrWhiteSpace(breadcrumbEntry.Name)) { breadcrumbNavigation.Add(breadcrumbEntry); } } ViewBag.FolderPath = FolderPath; ViewBag.FolderMenu = breadcrumbNavigation; if (TempData["LibraryPostbackMessage"] != null) { ViewBag.LibraryPostbackMessage = TempData["LibraryPostbackMessage"]; } return PartialView(libraryItems); }
public ActionResult Index(string FolderPath) { if (String.IsNullOrWhiteSpace(FolderPath)) { FolderPath = ""; } FolderPath = CleanFolderPath(FolderPath); // A combination of our server path and the specific folder we are looking in for files string ActualPath = Path.Combine(ActualRootPath, FolderPath); //This means that the path that was specified does not exist so I have to take you somewhere, and that would be the root of the file library. if (!Directory.Exists(ActualPath)) { return RedirectToAction("Index"); } // Add each directory and file to the list so we can work with it var libraryItems = new List<FileLibraryItem>(); // Uses the combination defined above as our working directory DirectoryInfo APDI = new DirectoryInfo(ActualPath); // List all the directories withing the current folder path libraryItems.AddRange(APDI.GetDirectories("*", SearchOption.TopDirectoryOnly).Select(directory => new FileLibraryItem() { Name = directory.Name, Path = directory.FullName.Replace(ActualRootPath, "").Replace("\\", "/"), IsDirectory = true })); // Lists all the individual files within the current folder path libraryItems.AddRange(APDI.GetFiles("*", SearchOption.TopDirectoryOnly).Select(file => new FileLibraryItem() { Name = file.Name, Size = file.Length, Path = RootPath + CleanFolderPath(FolderPath) + "/" + file.Name, FileType = MimeMapping.GetMimeMapping(file.FullName), DateAdded = file.LastWriteTime })); // Generate the breadcrumbs menu at the top of the page List<Breadcrumb> breadcrumbNavigation = new List<Breadcrumb>(); string[] folders = FolderPath.Split('\\'); for (int i = 0; i < folders.Length; i++) { var breadcrumbEntry = new Breadcrumb() { Name = folders[i], Path = String.Empty }; for (int r = 0; r <= i; r++) { if (r != 0) { breadcrumbEntry.Path += "/"; } breadcrumbEntry.Path += folders[r]; } if (!String.IsNullOrWhiteSpace(breadcrumbEntry.Name)) { breadcrumbNavigation.Add(breadcrumbEntry); } } ViewBag.FolderPath = FolderPath; ViewBag.FolderMenu = breadcrumbNavigation; if (TempData["LibraryPostbackMessage"] != null) { ViewBag.LibraryPostbackMessage = TempData["LibraryPostbackMessage"]; } return View(libraryItems); }