public IActionResult EditArticle(AddNewArticleGetViewModel displayArticle, Article article) { if (ModelState.IsValid) { var oldImagePath = displayArticle.ImagePath.Remove(0, 9); article.CategoryId = displayArticle.SelectedCat; string FileName = ""; string path_root = _hostingEnvironment.WebRootPath; if ((displayArticle?.Image?.Length > 0) && ((displayArticle?.Image?.ContentType == "image/jpeg") || (displayArticle?.Image?.ContentType == "image/jpg"))) { string path_Old_Image = path_root + "\\Images\\" + oldImagePath; string path_to_image = path_root + "\\Images\\" + displayArticle.Image.FileName; FileName = uploadFileRepository.UplaodFile(displayArticle.Image, "\\Images\\"); article.Image = @"~/images/" + FileName; System.IO.File.Delete(path_Old_Image); } else { article.Image = displayArticle.ImagePath; } if ((displayArticle?.Video?.Length > 0) && (displayArticle?.Video?.ContentType == "video/mp4")) { if (displayArticle.VideoPath == null) { } else { var oldVideoPath = displayArticle.VideoPath.Remove(0, 8); string path_Old_Image = path_root + "\\Video\\" + oldVideoPath; System.IO.File.Delete(path_Old_Image); } FileName = uploadFileRepository.UplaodFile(displayArticle.Video, "\\Video\\"); article.Video = @"~/video/" + FileName; } if ((displayArticle?.PDF?.Length > 0) && (displayArticle?.PDF?.ContentType == "application/pdf")) { if (displayArticle.PDFPath == null) { } else { var oldPdfPath = displayArticle.PDFPath.Remove(0, 6); string path_Old_Image = path_root + "\\Pdf\\" + oldPdfPath; System.IO.File.Delete(path_Old_Image); } FileName = uploadFileRepository.UplaodFile(displayArticle.PDF, "\\Pdf\\"); article.PDF = @"~/pdf/" + FileName; } articleRepository.Update(article); return(RedirectToAction(nameof(ListArticle))); } else { return(View(article)); } }
public ActionResult AddArticle(AddNewArticleGetViewModel model) { if (ModelState.IsValid) { var persiandate = new PersianDateTime(DateTime.Now); string UserId = userManager.GetUserId(HttpContext.User); Article article = new Article { Title = model.Title, Abstract = model.Abstract, Body = model.Body, PublishDate = persiandate.ToString(), Status = ArticleStatus.PrePublish, CategoryId = model.SelectedCat, Likes = 0, ViewCount = 0, AspNetUsersId = int.Parse(UserId) }; string FileName = ""; if ((model?.Image?.Length > 0) && ((model?.Image?.ContentType == "image/jpeg") || (model?.Image?.ContentType == "image/jpg"))) { FileName = uploadFileRepository.UplaodFile(model.Image, "\\Images\\"); article.Image = @"~/images/" + FileName; } if ((model?.Video?.Length > 0) && (model?.Video?.ContentType == "video/mp4")) { FileName = uploadFileRepository.UplaodFile(model.Video, "\\Video\\"); article.Video = @"~/video/" + FileName; } if ((model?.PDF?.Length > 0) && (model?.PDF?.ContentType == "application/pdf")) { FileName = uploadFileRepository.UplaodFile(model.PDF, "\\Pdf\\"); article.PDF = @"~/pdf/" + FileName; } articleRepository.Add(article); return(RedirectToAction(nameof(ListArticle))); } else { return(RedirectToAction(nameof(ListArticle))); } }