public async Task <IActionResult> UploadDocumentToFileSystem(IFormFile file) { var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\"); bool basePathExists = System.IO.Directory.Exists(basePath); if (!basePathExists) { Directory.CreateDirectory(basePath); } var fileName = Path.GetFileNameWithoutExtension(file.FileName); var filePath = Path.Combine(basePath, file.FileName); // var extension = Path.GetExtension(file.FileName); if (!System.IO.File.Exists(filePath)) { using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var fileModel = new DocumentOnFileSystemModel { Name = fileName, FileType = file.ContentType, SubmissionDate = DateTime.UtcNow, FilePath = filePath }; _documentRepository.UploadDocumentToFileSystem(fileModel); } return(new OkResult()); }
public bool UploadDocumentToFileSystem(DocumentOnFileSystemModel doc) { try { _dbContext.DocumentOnFileSystem.Add(doc); Save(); return(true); } catch (Exception) { return(false); } }
public void UploadFileInFileSystem() { var fileMock = new Mock <IFormFile>(); //Setup mock file using a memory stream var content = "Hello World from a Fake File"; var fileName = "test.pdf"; var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(content); writer.Flush(); ms.Position = 0; fileMock.Setup(_ => _.OpenReadStream()).Returns(ms); fileMock.Setup(_ => _.FileName).Returns(fileName); fileMock.Setup(_ => _.Length).Returns(ms.Length); var file = fileMock.Object; var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\"); bool basePathExists = System.IO.Directory.Exists(basePath); if (!basePathExists) { Directory.CreateDirectory(basePath); } var fileName1 = Path.GetFileNameWithoutExtension(file.FileName); var filePath = Path.Combine(basePath, file.FileName); // var extension = Path.GetExtension(file.FileName); if (!System.IO.File.Exists(filePath)) { using (var stream = new FileStream(filePath, FileMode.Create)) { file.CopyToAsync(stream); } var fileModel = new DocumentOnFileSystemModel { Name = fileName, FileType = file.ContentType, SubmissionDate = DateTime.UtcNow, FilePath = filePath }; Assert.IsTrue(_documentRepository.UploadDocumentToFileSystem(fileModel), "File Uploaded To Database"); } }
public IActionResult CheckDocumentOnFileSystem(int id) { DocumentOnFileSystemModel str = _obj.FindDocumentOnFileSystemModel(id); return(new OkObjectResult(str)); }