public IActionResult AddPhoto(string title, string description, IFormFile file) { Photo p = new Photo { Title = title, Description = description, UserName = User.Identity.Name, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now }; if (file != null && file.Length > 0) { p.FileName = file.FileName; p.ImageMimeType = file.ContentType; p.FileData = new byte[file.Length]; using (var stream = file.OpenReadStream()) { stream.Read(p.FileData, 0, (int)file.Length); } // Add the photo to the database. _dbContext.Photos.Add(p); _dbContext.SaveChanges(); return(Ok()); } ModelState.AddModelError("", "File is not received by the server!"); string strErrors = _helperService.GetModelStateErrorsString(ModelState); return(BadRequest(strErrors)); }
public ActionResult Create(Photo p, HttpPostedFileBase inputImage) { if (!ModelState.IsValid) { return(View(p)); } p.UserName = User.Identity.Name; p.CreatedDate = DateTime.Now; p.ModifiedDate = DateTime.Now; // If there is an image, upload it. if (inputImage != null) { p.ImageMimeType = inputImage.ContentType; p.FileName = System.IO.Path.GetFileName(inputImage.FileName); p.FileData = new byte[inputImage.ContentLength]; inputImage.InputStream.Read(p.FileData, 0, inputImage.ContentLength); } // Add the photo to the database. context.Photos.Add(p); context.SaveChanges(); return(RedirectToAction("AllPhotos", "Home")); }
public void UpdateBd(string photoName, int id) { Models.PhotoModel user = new Models.PhotoModel(); using (var db = new PhotoDbContext()) { var photo = db.Photos.Create(); photo.PhotoPath = photoName; photo.AlbumId = id; db.Photos.Add(photo); db.SaveChanges(); } }
public ActionResult Create(HttpPostedFileBase upload, AddImage img) { if (upload != null) { string fileName = System.IO.Path.GetFileName(upload.FileName); if (System.IO.File.Exists((Server.MapPath("/Files/" + fileName)))) { ViewBag.File = "Filename already exists: " + fileName; } else { if (ModelState.IsValid) { upload.SaveAs(Server.MapPath("/Files/" + fileName)); img.ImagePath = fileName; db.Images.Add(img); db.SaveChanges(); ViewBag.File = "File uploaded " + fileName; } } } ViewBag.Files = System.IO.Directory.GetFiles(Server.MapPath("/Files/")); return(View("Index")); }
/// <summary> /// /// </summary> /// <returns></returns> public bool Save() { return(_photoDbContext.SaveChanges() >= 0); }