Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
 public bool UploadDocumentToFileSystem(DocumentOnFileSystemModel doc)
 {
     try
     {
         _dbContext.DocumentOnFileSystem.Add(doc);
         Save();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        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");
            }
        }
Exemplo n.º 4
0
        public IActionResult CheckDocumentOnFileSystem(int id)
        {
            DocumentOnFileSystemModel str = _obj.FindDocumentOnFileSystemModel(id);

            return(new OkObjectResult(str));
        }