コード例 #1
0
        public ActionResult Create(string FolderPath, string directoryName)
        {
            directoryName = CleanFolderPath(directoryName.Trim());

            if (String.IsNullOrWhiteSpace(directoryName))
            {
                TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                {
                    Type = LibraryPostbackMessage.MessageType.warning,
                    Message = "The folder name cannot be blank. Please enter a folder name."
                };
            }
            else
            {
                string newFolderPath = ActualRootPath;
                if (!String.IsNullOrWhiteSpace(FolderPath))
                {
                    newFolderPath += CleanFolderPath(FolderPath) + "\\";
                }
                newFolderPath += directoryName;

                if (Directory.Exists(newFolderPath))
                {
                    TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                    {
                        Message = "There is already a folder with that name."
                    };
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(newFolderPath);
                    }
                    catch
                    {
                        TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                        {
                            Message = "Unable to create the specified folder."
                        };
                    }
                }
            }
            return RedirectToAction("Index", "FileLibrary", new { FolderPath = FolderPath });
        }
コード例 #2
0
        public ActionResult Upload(string FolderPath, List<HttpPostedFileBase> fileupload)
        {
            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                if (TempData["LibraryPostbackMessage"] != null) { break; } // Don't continue if there is already an error on a prior file
                try
                {
                    string newfilePath = ActualRootPath;
                    if (!String.IsNullOrWhiteSpace(FolderPath))
                    {
                        newfilePath += CleanFolderPath(FolderPath) + "\\";
                    }
                    newfilePath += file.FileName;

                    // Verify the file doesn't already exist in the current folder
                    if (!System.IO.File.Exists(newfilePath))
                    {
                        file.SaveAs(newfilePath);
                    }
                    else
                    {
                        TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                        {
                            Message = "One or more files you attempted to upload already exists in this folder. Please rename the files or try uploading to a new folder."
                        };
                    }
                }
                catch
                {
                    TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                    {
                        Message = "The file upload was unsuccessful. Please try again."
                    };
                }
            }

            if (TempData["LibraryPostbackMessage"] == null) //If I left this in the foreach then I might override an earlier message.
            {
                TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                {
                    Type = LibraryPostbackMessage.MessageType.success,
                    Message = "The file upload was successful."
                };
            }
            return RedirectToAction("Index", "FileLibrary", new { folderPath = FolderPath });
        }
コード例 #3
0
        public ActionResult Delete(string DeletePath)
        {
            string ParentFolder = String.Empty;
            if (String.IsNullOrWhiteSpace(DeletePath))
            {
                TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                {
                    Message = "Please select an item to delete."
                };
            }
            else
            {
                string FolderPath = DeletePath.Replace(RootPath, "").Replace("/", "\\");
                if (FolderPath.Length > 0 && FolderPath.StartsWith("\\"))
                {
                    FolderPath = FolderPath.Substring(1, FolderPath.Length - 1);
                }
                FolderPath = ActualRootPath + FolderPath;
                if (Directory.Exists(FolderPath) || System.IO.File.Exists(FolderPath))
                {
                    ParentFolder = Path.GetDirectoryName(FolderPath) + "\\";
                    FileAttributes attr = System.IO.File.GetAttributes(FolderPath);

                    if (attr.HasFlag(FileAttributes.Directory)) //Check to see if the delete path is a folder
                    {
                        try
                        {
                            Directory.Delete(FolderPath, true);
                            TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                            {
                                Type = LibraryPostbackMessage.MessageType.success,
                                Message = "The folder and it's contents was removed successfully."
                            };
                        }
                        catch
                        {
                            TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                            {
                                Message = "The folder was unable to be removed."
                            };
                        }
                    }
                    else //The delete path is a file
                    {
                        try
                        {
                            System.IO.File.Delete(FolderPath);
                            TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                            {
                                Type = LibraryPostbackMessage.MessageType.success,
                                Message = "The file was removed successfully."
                            };
                        }
                        catch
                        {
                            TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                            {
                                Message = "The file was unable to be removed."
                            };
                        }
                    }
                }
                else
                {
                    TempData["LibraryPostbackMessage"] = new LibraryPostbackMessage()
                    {
                        Message = "This file does not exist or has already been deleted."
                    };
                }
            }
            return RedirectToAction("Index", "FileLibrary", new { FolderPath = ParentFolder.Replace(ActualRootPath, String.Empty) });
        }