public async Task <IActionResult> Edit(int id, [Bind("cardID,baslik,aciklama,icerik,imgFile")] homeCards proje, string fname) { if (id != proje.cardID) { return(NotFound()); } if (ModelState.IsValid) { try { string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(proje.imgFile.FileName); string extension = Path.GetExtension(proje.imgFile.FileName); proje.imgYol = fileName = fileName + DateTime.Now.ToString("yymmssffff") + extension; //proje.imgYol = fileName = proje.imgFile.FileName; string path = Path.Combine(wwwRootPath + "/cardImg/", fileName); proje.cardID = id; using (var fileStream = new FileStream(path, FileMode.Create)) { await proje.imgFile.CopyToAsync(fileStream); } var imgid = await _context.homeCards.FindAsync(id); _context.homeCards.Remove(imgid); fname = Path.Combine(wwwRootPath + "/cardImg/", fileName); FileInfo fi = new FileInfo(fname); if (fi.Exists) { System.IO.File.Delete(fname); fi.Delete(); } createImage(proje); _context.Update(proje); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!homeCardsExists(proje.cardID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(proje)); }
public async void createImage(homeCards proje) { string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(proje.imgFile.FileName); string extension = Path.GetExtension(proje.imgFile.FileName); proje.imgYol = fileName = fileName + DateTime.Now.ToString("yymmssffff") + extension; //proje.imgYol = fileName = proje.imgFile.FileName; string path = Path.Combine(wwwRootPath + "/cardImg/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await proje.imgFile.CopyToAsync(fileStream); } }
public async Task <IActionResult> Create(IFormFile file, [Bind("cardID,baslik,aciklama,icerik,imgFile")] homeCards proje) { if (ModelState.IsValid) { //save image to wwwroot/image createImage(proje); //Insert record _context.Add(proje); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(proje)); }