Exemple #1
0
        public async Task <IActionResult> UploadAsync(string creatorFirstName, string creatorLastName, string title, IFormFile document)
        {
            if (string.IsNullOrWhiteSpace(creatorFirstName) ||
                string.IsNullOrWhiteSpace(creatorLastName) ||
                string.IsNullOrWhiteSpace(title) ||
                document.Length > DocumentMaxSize)
            {
                return(this.View());
            }

            var extension  = Path.GetExtension(document.FileName);
            var trimmedExt = extension.Substring(1, extension.Length - 1);

            if (extension != ".pdf" &&
                extension != ".doc" &&
                extension != ".docx" &&
                extension != ".xls" &&
                extension != ".xlsx")
            {
                return(this.View());
            }
            var fileContents = await document.ToByteArrayAsync();

            var modelForDb = new DocumentCreateServiceModel
            {
                FirstName = creatorFirstName,
                LastName  = creatorLastName,
                Title     = $"{title} - {trimmedExt.ToLower()}",
                Created   = DateTime.UtcNow,
                Document  = fileContents,
            };

            bool isCreated = await this.documentService.UploadDocumentAsync(modelForDb);

            if (isCreated)
            {
                return(this.RedirectToAction("Index", "Dashboard", new { area = "Administration" }));
            }
            else
            {
                return(this.View());
            }
        }
        public async Task <bool> UploadDocumentAsync(DocumentCreateServiceModel model)
        {
            var document = new Document
            {
                FirstName       = model.FirstName,
                LastName        = model.LastName,
                Title           = model.Title,
                Created         = model.Created,
                DocumentContent = model.Document
            };

            await this.context.Documents.AddAsync(document);

            int result = await this.context.SaveChangesAsync();

            if (result > 0)
            {
                return(true);
            }

            return(false);
        }