private void UploadPictures(IFormFileCollection pictures, int newDestinationId) { string[] supportedTypes = { ".jpg", ".jpeg", ".png" }; foreach (IFormFile picture in pictures) { string fileExtension = Path.GetExtension(picture.FileName); if (supportedTypes.Contains(fileExtension)) { string imagesDirectoryPath = "/images/destinations"; string webRootPath = _hostingEnvironment.WebRootPath; // generate unique GUID (globally unique identifier) for file to upload (prevent filename collisions) string picturePath = string.Format(@"{0}/{1}/{2}{3}", imagesDirectoryPath, newDestinationId.ToString(), Guid.NewGuid(), fileExtension); string serverSavePath = string.Format(@"{0}{1}/{2}", webRootPath, imagesDirectoryPath, newDestinationId.ToString()); Directory.CreateDirectory(serverSavePath); picture.CopyTo(new FileStream(webRootPath + picturePath, FileMode.Create)); ImagePath imagePathEntry = new ImagePath { DestinationId = newDestinationId, Path = picturePath }; _service.CreateImagePath(imagePathEntry); } } }