コード例 #1
0
        // GET: WatchParties/Create
        public IActionResult Create()
        {
            UploadWatchPartyPicViewModel viewModel = new UploadWatchPartyPicViewModel();

            viewModel.watchParty = new WatchParty();
            ViewData["TeamId"]   = new SelectList(_context.Team, "TeamId", "Name");

            return(View(viewModel));
        }
コード例 #2
0
        public async Task <IActionResult> Create(UploadWatchPartyPicViewModel viewModel)
        {
            ModelState.Remove("watchParty.UserId");

            // adding current userId
            var user = await GetCurrentUserAsync();

            viewModel.watchParty.UserId = user.Id;


            if (ModelState.IsValid)
            {
                if (viewModel.ImageFile != null)
                {
                    // don't rely on or trust the FileName property without validation
                    //**Warning**: The following code uses `GetTempFileName`, which throws
                    // an `IOException` if more than 65535 files are created without
                    // deleting previous temporary files. A real app should either delete
                    // temporary files or use `GetTempPath` and `GetRandomFileName`
                    // to create temporary file names.
                    var fileName = Path.GetFileName(viewModel.ImageFile.FileName);
                    Path.GetTempFileName();
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images/partypic", fileName);
                    //var filePath = Path.GetTempFileName();
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await viewModel.ImageFile.CopyToAsync(stream);

                        // validate file, then move to CDN or public folder
                    }

                    viewModel.watchParty.ImagePath = viewModel.ImageFile.FileName;
                }



                _context.Add(viewModel.watchParty);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["TeamId"] = new SelectList(_context.Team, "TeamId", "Name", viewModel.watchParty.TeamId);

            return(View(viewModel.watchParty));
        }