public async Task <IActionResult> PutMedia([FromRoute] int id, [FromBody] Media media) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != media.MediaId) { return(BadRequest()); } _context.Entry(media).State = EntityState.Modified; try { _repo.Update(media); var save = await _repo.SaveAsync(media); } catch (DbUpdateConcurrencyException) { if (MediaExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Edit([Bind(Include = "EmployeeAccount")] ViewTeacherAccount objViewTeacherAccount, FormCollection collection, HttpPostedFileBase image) { SMS.Models.MediaModels.Image objImage = new SMS.Models.MediaModels.Image(); if (image != null) { ////attach the uploaded image to the object before saving to Database //objImage.ImageMimeType = image.ContentLength; objImage.ImageFile = new byte[image.ContentLength]; image.InputStream.Read(objImage.ImageFile, 0, image.ContentLength); //Save image to file var filename = image.FileName; var filePathOriginal = Server.MapPath("/Data/Image"); var filePathThumbnail = Server.MapPath("/Data/Thumbnails"); string savedFileName = Path.Combine(filePathOriginal, filename); image.SaveAs(savedFileName); //Read image back from file and create thumbnail from it var imageFile = Path.Combine(Server.MapPath("~/Data/Image"), filename); using (var srcImage = System.Drawing.Image.FromFile(imageFile)) using (var newImage = new Bitmap(100, 100)) using (var graphics = Graphics.FromImage(newImage)) using (var stream = new MemoryStream()) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.DrawImage(srcImage, new Rectangle(0, 0, 200, 200)); newImage.Save(stream, ImageFormat.Png); var thumbNew = File(stream.ToArray(), "image/png"); //artwork.ArtworkThumbnail = thumbNew.FileContents; } SMS.Models.MediaModels.Image objImg = imagedb.Images.FirstOrDefault(p => p.TrackingId == objViewTeacherAccount.EmployeeAccount.NidOrBirtgRegNo); if (objImg == null) { objImage.TrackingId = objViewTeacherAccount.EmployeeAccount.NidOrBirtgRegNo; imagedb.Images.Add(objImage); imagedb.SaveChanges(); } else { objImg.TrackingId = objViewTeacherAccount.EmployeeAccount.NidOrBirtgRegNo; objImg.ImageFile = objImage.ImageFile; imagedb.Entry(objImg).State = EntityState.Modified; imagedb.SaveChanges(); } } if (ModelState.IsValid) { db.Entry(objViewTeacherAccount.EmployeeAccount).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } objViewTeacherAccount.EmployeeEducationalQualifications = db.EmployeeEducationalQualifications.Where(s => s.NidOrBirtgRegNo == objViewTeacherAccount.EmployeeAccount.NidOrBirtgRegNo).ToList(); objViewTeacherAccount.Designation = new SelectList(db.Designations, "DesignationCode", "Name", objViewTeacherAccount.Designation); objViewTeacherAccount.Subject = new SelectList(db.Subjects, "SubjectCode", "Name", objViewTeacherAccount.Subject); return(View(objViewTeacherAccount)); }