public ActionResult Upload(HttpPostedFileBase file, int?moduleId) { var module = _context.Modules.SingleOrDefault(m => m.Id == moduleId); if (module == null) { return(HttpNotFound()); } if (file.ContentLength > 0) { if (Image.IsContentTypeValid(file.ContentType)) { var fileName = Path.GetFileName(file.FileName); var image = _context.Images.Add(new Image { ContentType = file.ContentType }); _context.SaveChanges(); var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), image.ToString()); file.SaveAs(path); module.ImageId = image.Id; _context.SaveChanges(); } else { var viewModel = new NewImageViewModel { Image = new Image(), ErrorMessage = "This image is not a valid format. Please choose a jpeg or png image." }; View("New", viewModel); } } return(RedirectToAction("Index", "Modules")); }
// GET /images/new?moduleId={moduleId} public ActionResult New(int moduleId) { var module = _context.Modules.SingleOrDefault(m => m.Id == moduleId); if (module == null) { return(HttpNotFound()); } var viewModel = new NewImageViewModel { Image = new Image(), ModuleId = moduleId }; return(View(viewModel)); }
public ActionResult addImage(string firstName, string lastName, HttpPostedFileBase image) { string fileName = Guid.NewGuid() + Path.GetExtension(image.FileName); image.SaveAs(Server.MapPath("~/Images") + "/" + fileName); Image i = new Image { FirstName = firstName, LastName = lastName, FileName = fileName, DateUploaded = DateTime.Now, ViewCount = 0 }; Manager manager = new Manager(Properties.Settings.Default.ConStr); int Id = manager.AddImage(i); var vm = new NewImageViewModel(); vm.ImageId = Id; return(View(vm)); }
private static void GetImages(XElement canvas, Dictionary <UIElement, CanvasPosition> list) { var images = canvas.Descendants(ns + "Image").ToList(); // poprawić odczyt img z pliku foreach (var img in images) { try { Image image = new Image(); NewImageViewModel newImageViewModel = new NewImageViewModel(); newImageViewModel.Name = img.Attribute("Name").Value; newImageViewModel.ImageSource = img.Attribute("Source").Value.Replace("file:///", ""); image.Name = newImageViewModel.Name; image.Source = new BitmapImage(new Uri(newImageViewModel.ImageSource)); var left = img.Attribute("Canvas.Left").Value; var top = img.Attribute("Canvas.Top").Value; var canvasLeft = Convert.ToDouble(StrToDouble(left)); var canvasTop = Convert.ToDouble(StrToDouble(top)); image.DataContext = newImageViewModel; list.Add(image, new CanvasPosition(canvasLeft, canvasTop)); } catch (Exception ex) { throw new Exception("Błąd podczas przetwarzania komponentów typu Image. " + ex.Message); } } }