FileManager Table of contents FileManager Table of contents General info Technologies Setup Model Returned Status FileMessage Methods Get Get name Get size Create Delete Exceptions Code Examples Get GetList GetName GetNames GetSize Create Delete General info FileManager is the library to management files in your project. Technologies C# EntityFramework - version 6.2.0 Setup Add following config to your web.config: <configuration> <configSections> <section name="FileManager" type="FileManagerLibrary.Config.Section"/> </configSections> <FileManager> <DbConfig connectionString="CONNECTION STRING TO DATABASE"/> <DirectoryConfig path="~/BASE DIRECTORY"/> <!-- Optional parameter --> <CustomConfig maxFileSize="MAX FILE SIZE IN KB"/> </FileManager> </configuration> Model public class StorageFile { public long Id { get; set; } [Required] public Guid PublicId { get; set; } [Required] public DateTime DateCreatedUtc { get; set; } [Required] public string CreatedBy { get; set; } [Required] public string Name { get; set; } [Required] public string ContentType { get; set; } [Required] public int ContentLength { get; set; } [Required] public string Directory { get; set; } [NotMapped] public FileStream FileStream { get; set; } } Returned Status public enum Status { NotExists = 0, Exists = 1, SizeIsToBig = 2, Ok = 9 } FileMessage public class FileMessage { public HttpPostedFileBase File { get; set; } public StorageFile.Status Status { get; set; } } Methods Get Method Returned type Parameters Description name withDependencies: long id, true: return record from bool database with file from disk StorageFile Get withDependencies false: return only record from = false database withDependencies: Guid publicId, true: return record from bool database with file from disk StorageFile Get withDependencies false: return only record from = false database withDependencies: List<long> ids, true: return records from bool database with files from disk List<StorageFile> GetList withDependencies false: return only records from = false database List<Guid> withDependencies: publicIds, true: return records from List<StorageFile> GetList bool database with files from disk withDependencies false: return only records from = false database Get name Returned type Method name Parameters Description string GetName long id string GetName Guid publicId List<string> GetNames List<long> ids List<string> GetNames List<Guid> publicIds Get size Returned type Method name Parameters Description int? GetSize long id Create Returned type Method name Parameters Description HttpPostedFileBase file, StorageFile.Status Create string directory, string createdBy HttpPostedFileBase[] files, List<FileMessage> Create string directory, string createdBy Delete Returned type Method name Parameters Description StorageFile.Status Delete Guid publicId StorageFile.Status Delete long id StorageFile.Status Delete StorageFile record StorageFile.Status Delete List<long> ids StorageFile.Status Delete List<Guid> publicIds StorageFile.Status Delete List<StorageFile> files Exceptions Name Description FileManagerException If any exception occurs in methods Code Examples Get try { var file = FilesRepository.Get(2, withDependencies: true); } catch (FileManagerException ex) { throw; } GetList var testList = new List<long>(){1, 2, 3}; try { var files = FilesRepository.GetList(testList, withDependencies: true); } catch (FileManagerException ex) { throw; } GetName try { var fileName = FilesRepository.GetName(3); } catch (FileManagerException ex) { throw; } GetNames var testList = new List<long>(){1, 2, 3}; try { var fileNames = FilesRepository.GetNames(testList); } catch (FileManagerException ex) { throw; } GetSize try { var fileSize = FilesRepository.GetSize(3); //possible null } catch (FileManagerException ex) { throw; } Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Test(HttpPostedFileBase file) { try { var status = FilesRepository.Create(file, "directory", "createdBy"); if (status != StorageFile.Status.Ok) { switch (status) { case StorageFile.Status.Exists: break; case StorageFile.Status.SizeIsToBig: break; } } } catch (FileManagerException ex) { throw; } return View(); } Delete try { //var file = FilesRepository.Get(3); //var status = FilesRepository.Delete(file); //var testList = new List<long>(){1, 2, 3}; //var files = FilesRepository.GetList(testList); //var status = FilesRepository.Delete(files); //var testList = new List<long>(){1, 2, 3}; //var status = FilesRepository.Delete(testList); var status = FilesRepository.Delete(3); if (status != StorageFile.Status.Ok) { switch (status) { case StorageFile.Status.NotExists: break; } } } catch (FileManagerException ex) { throw; }
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-