public async Task <IActionResult> OnPostAsync() { if (ModelState.IsValid == false) { return(Page()); } if (CoverPhoto == null || CoverPhoto.Length == 0) { return(Content("coverPhoto not selected")); } //取得現在的靜態檔案路徑 var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", CoverPhoto.FileName); await using (var stream = new FileStream(path, FileMode.Create)) { await CoverPhoto.CopyToAsync(stream); } var article = new Article { Id = Guid.NewGuid(), Body = ArticleForPage.Body, Tags = string.Join(",", ArticleForPage.TagList), Title = ArticleForPage.Title, CoverPhoto = path, CreateDate = CreateDate.Date + CreateTime.TimeOfDay }; await _context.Articles.AddAsync(article); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (ModelState.IsValid == false) { return(Page()); } var path = ""; if (CoverPhoto != null && CoverPhoto.Length != 0) { //取得現在的靜態檔案路徑 path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", CoverPhoto.FileName); await using var stream = new FileStream(path, FileMode.Create); await CoverPhoto.CopyToAsync(stream); } var article = await UpdateArticle(path); _context.Attach(article).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArticlesExists(ArticleForPage.Id)) { return(NotFound()); } throw; } return(RedirectToPage("./Index")); }