public async Task <IActionResult> Edit(IFormFile fileobj, ImageCrudClass icc, string fname, int id) { if (ModelState.IsValid) { var getimagedetails = await _adb.Saveimg.FindAsync(id); _adb.Saveimg.Remove(getimagedetails); fname = Path.Combine(_iweb.WebRootPath, "Images", getimagedetails.Imgname); FileInfo fi = new FileInfo(fname); if (fi.Exists) { System.IO.File.Delete(fname); fi.Delete(); } var imgext = Path.GetExtension(fileobj.FileName); if (imgext == ".jpg" || imgext == ".gif") //Here i am allowing only image files. { var uploading = Path.Combine(_iweb.WebRootPath, "Images", fileobj.FileName); var stream = new FileStream(uploading, FileMode.Create); await fileobj.CopyToAsync(stream); stream.Close(); icc.Imgname = fileobj.FileName; icc.Imgpath = uploading; _adb.Update(icc); await _adb.SaveChangesAsync(); } } return(RedirectToAction("Index")); }
public async Task <IActionResult> Index(IFormFile fileobj, ImageCrudClass icc) { var imgext = Path.GetExtension(fileobj.FileName); if (imgext == ".jpg" || imgext == ".gif") //Here i am allowing only image files. { var uploading = Path.Combine(_iweb.WebRootPath, "Images", fileobj.FileName); var stream = new FileStream(uploading, FileMode.Create); await fileobj.CopyToAsync(stream); stream.Close(); icc.Imgname = fileobj.FileName; icc.Imgpath = uploading; await _adb.Saveimg.AddAsync(icc); // Here Saveimg is table name. await _adb.SaveChangesAsync(); } return(RedirectToAction("Index")); }