コード例 #1
0
        public IActionResult Create(EmployCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null; //Photo file name
                if (model.Photos != null && model.Photos.Count > 0)
                {
                    foreach (IFormFile photo in model.Photos)
                    {
                        string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                        uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                        string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                        photo.CopyTo(new FileStream(filePath, FileMode.Create));
                    }
                }

                Employee newEmployee = new Employee
                {
                    Name       = model.Name,
                    Email      = model.Email,
                    Department = model.Department,
                    PhotoPath  = uniqueFileName
                };

                _employeeRepository.Add(newEmployee);
                return(RedirectToAction("details", new { id = newEmployee.Id }));
            }
            return(View());
        }
コード例 #2
0
        private string ProcessUploadedFile(EmployCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photos != null && model.Photos.Count > 0)
            {
                foreach (IFormFile photo in model.Photos)
                {
                    string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }
            }

            return(uniqueFileName);
        }