Example #1
0
        private string ProcessUploadedFile(StatementCreateViewModel model)
        {
            var uniqueFileName = string.Empty;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(HostingEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
Example #2
0
        public async Task <IActionResult> Create(StatementCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedFile(model);

                var newStatement = new Statement
                {
                    Id          = Guid.NewGuid(),
                    PhotoPath   = uniqueFileName,
                    Title       = model.Title,
                    Description = model.Description,
                    PhoneNumber = model.PhoneNumber
                };


                await Repository.Statements.CreateStatementAsync(newStatement);

                return(RedirectToAction("Details", new { Id = newStatement.Id }));
            }
            return(View(model));
        }