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()); } }
void ProcessFiles(IEnumerable <string> files, CancellationToken token) { Dictionary <string, PatchInfo> patches = new Dictionary <string, PatchInfo>(); foreach (string fullName in files) { if (token.IsCancellationRequested) { break; } PatchInfo patch; string directory = Path.GetDirectoryName(fullName); if (!patches.TryGetValue(directory, out patch)) { string[] patchFiles = Directory.GetFiles(directory, "*.patch"); if (patchFiles.Length > 0) { patch = PatchInfo.Read(patchFiles[0]); } else { patch = null; } patches.Add(directory, patch); } string imagePath = ImagePathBuilder.BuildPath(this.outputDir, fullName, this.rules, patch); if (!File.Exists(imagePath) && File.Exists(fullName)) { try { string targetDirectory = Path.GetDirectoryName(imagePath); if (!Directory.Exists(targetDirectory)) { Directory.CreateDirectory(targetDirectory); } File.Copy(fullName, imagePath, false); } catch (Exception e) { AppendLog("The file " + fullName + " has not copied.", e.Message, e.StackTrace); } } } }
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))); } }