Example #1
0
        public async Task <IActionResult> UploadFileAsync(FileUploadViewModel model)
        {
            if (!ModelState.IsValid)
            {
                string errorMessages = String.Join("; ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)) + ". ";

                TempData["ActionMessage"] = $"[FAILED] The image { model.File.FileName } cannot be uploaded. { errorMessages }Please try again.";
            }
            else
            {
                var newFileUpload = await _fileProcessor.InsertFileRecordToDatabaseWithImageUploadingAsync(
                    model.File,
                    _configuration["ConnectionString:AzureStorage"],
                    "uploaded-photos",
                    await GetCurrentUserAsync());

                await _db.FileUploads.AddAsync(newFileUpload);

                await _db.SaveChangesAsync();

                TempData["ActionMessage"] = $"The image { model.File.FileName } has been uploaded.";
            }

            return(RedirectToAction("Index"));
        }