public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (PicturFile != null && PicturFile.Length > 0) { if (StorageHelper.IsImage(PicturFile)) { var uploadedPic = await _commandHelper.UploadFileAsync(PicturFile, FileType.Image); Blog.PhotoUrl = uploadedPic.FileUrl; } else { ModelState.AddModelError("PhotoUrl", "Please select image type file"); return(Page()); } } if (Request.Form.Files.Count > 0) { Attachments = Request.Form.Files.Where(x => x.Name.Contains("Attachments")).ToList(); if (Attachments.Count > 0) { Blog.Attachments = new Attachment[Attachments.Count]; for (var i = 0; i < Attachments.Count; i++) { Blog.Attachments[i] = await _commandHelper.UploadFileAsync(Attachments[i], FileType.File); } } } Blog.CreationDate = DateTime.Now; await _context.AddAsync(Blog); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } Blog = await _context.GetByIdAsync(Id); Entity.PhotoUrl = Blog.PhotoUrl; if (PicturFile != null && PicturFile.Length > 0) { if (StorageHelper.IsImage(PicturFile)) { var uploadedPic = await _commandHelper.UploadFileAsync(PicturFile, FileType.Image); Entity.PhotoUrl = uploadedPic.FileUrl; } else { ModelState.AddModelError("PhotoUrl", "Please select image type file"); return(Page()); } } Attachment[] newAttachments; var attachmentLength = Entity.Attachments == null ? 0 : Entity.Attachments.Length; if (Request.Form.Files.Count > 0) { Attachments = Request.Form.Files.Where(x => x.Name.Contains("Attachments")).ToList(); if (Attachments.Count > 0) { newAttachments = new Attachment[attachmentLength + Attachments.Count]; for (var i = 0; i < Attachments.Count; i++) { newAttachments[i] = await _commandHelper.UploadFileAsync(Attachments[i], FileType.File); } } else { newAttachments = new Attachment[attachmentLength]; } } else { newAttachments = new Attachment[attachmentLength]; } var matchedAttachment = _commandHelper.GetMatchedAttachment(Blog.Attachments, Entity.Attachments); int index = 0; if (matchedAttachment != null) { for (int i = Attachments.Count; i < newAttachments.Length; i++) { newAttachments[i] = matchedAttachment[index]; index++; } } Entity.Attachments = newAttachments; await _context.UpdateAsync(Entity); return(RedirectToPage("/Blog/Details", new { Id = Id })); }