示例#1
0
        public ActionResult Edit(News news, IFormFile UploadFile)
        {
            News _news = _context.News.Find(news.Id);

            try
            {
                //Loading/show current picture
                string oldImagePath = _news.PictureUrl;
                string oldImageGuid = _news.PicGuid;
                //ViewBag.imagePathAndName = imagePathAndName;


                if (UploadFile != null) //&& (UploadFile.Length > 0))
                {
                    var    fileExtension     = Path.GetExtension(UploadFile.FileName);
                    string pictureFolderPath = ImagePathBuilder.NewPath(_env.WebRootPath);
                    var    _picGuid          = Guid.NewGuid().ToString();
                    var    picturePath       = (Path.Combine(pictureFolderPath, (_picGuid))) + fileExtension;

                    using (FileStream stream = new FileStream(picturePath, FileMode.Create))
                    {
                        UploadFile.CopyTo(stream);
                    }

                    int    startPosToSelect    = picturePath.IndexOf("wwwroot\\Upload", System.StringComparison.CurrentCultureIgnoreCase);
                    string relativePicturePath = picturePath.Substring(startPosToSelect + 8); //cut after "wwwroot/"

                    _news.PictureUrl = relativePicturePath;
                    _news.PicGuid    = _picGuid;
                    _news.PicName    = news.PicName;
                }
                _news.Author   = news.Author;
                _news.Headline = news.Headline;
                _news.NewsText = news.NewsText;
                _news.Category = news.Category;
                //newsVM.Created = DateTime.Now;


                _context.Update(_news);
                _context.SaveChangesAsync();

                if ((oldImageGuid != null)) // || (news.PicGuid.Length > 0 )|| (news.PictureUrl.Length > 0))
                {
                    _deleteImages.Delete(oldImagePath, oldImageGuid);
                }


                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
示例#2
0
        public ActionResult Create(NewsVM newsVM, IFormFile UploadFile)
        {
            var _news = new Models.News();

            if (UploadFile != null) //&& (UploadFile.Length > 0))
            {
                var    fileExtension     = Path.GetExtension(UploadFile.FileName);
                string pictureFolderPath = ImagePathBuilder.NewPath(_env.WebRootPath);
                var    _picGuid          = Guid.NewGuid().ToString();
                var    picturePath       = (Path.Combine(pictureFolderPath, (_picGuid))) + fileExtension;

                using (FileStream stream = new FileStream(picturePath, FileMode.Create))
                {
                    UploadFile.CopyTo(stream);
                }

                int    startPosToSelect    = picturePath.IndexOf("wwwroot\\Upload", System.StringComparison.CurrentCultureIgnoreCase);
                string relativePicturePath = picturePath.Substring(startPosToSelect + 8); //cut after "wwwroot/"

                _news.PictureUrl = relativePicturePath;
                _news.PicGuid    = _picGuid;
                _news.PicName    = newsVM.PicName;
            }

            try
            {
                _news.Author   = newsVM.Author;
                _news.Headline = newsVM.Headline;
                _news.NewsText = newsVM.NewsText;
                _news.Created  = DateTime.Now;

                _context.News.Add(_news);
                _context.SaveChanges();

                return(RedirectToAction(nameof(View), new { _news.Id }));
                //return RedirectToAction(nameof(Index));
            }
            catch
            {
                return(RedirectToAction(nameof(Index)));
            }
        }