public ActionResult Edit([Bind(Include = "InfoId,Title,Description,Image, File")] DisplayInfo displayInfo, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var currentInfo = db.DisplayInfo.Find(displayInfo.InfoId);
                currentInfo.Title       = displayInfo.Title;
                currentInfo.Description = displayInfo.Description;

                if (file != null && file.ContentLength > 0)
                {
                    var img = ImageUploadController.ImageBytes(file, out string convertedLogo);
                    currentInfo.Image = img;
                    currentInfo.File  = file.FileName;
                }
                else
                {
                    currentInfo.Image = displayInfo.Image;
                    currentInfo.File  = displayInfo.File;
                }
                db.Entry(currentInfo).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(displayInfo));
        }
        //The ModelState variable only holds three key/value pairs in its dictionary after POST.
        //They are Title, Description and File (which is the file name, and should so be called.
        //Not sure whether or not Image should be in the Include parameter of the Bind method...

        public ActionResult Create([Bind(Include = "InfoId,Title,Description,File")] DisplayInfo displayInfo, HttpPostedFileBase file)
        {
            byte[] image = ImageUploadController.ImageBytes(file, out string _64);
            displayInfo.Image = image;
            displayInfo.File  = file.FileName;

            if (ModelState.IsValid)
            {
                db.DisplayInfo.Add(displayInfo);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(displayInfo));
        }
Exemple #3
0
        public ActionResult UploadImage(HttpPostedFileBase file)
        {
            try
            {
                //Using Helpers.ImageUploader.ImageBytes to get the byte[] representation of the file
                //and extracting the string representation as a returned out-parameter
                string imageBase64;
                byte[] imageBytes = ImageUploadController.ImageBytes(file, out imageBase64);

                //Add the base64 representation of the image to the ViewBag to be accessed by the View
                ViewBag.ImageData = String.Format("data:image/png;base64,{0}", imageBase64);

                ViewBag.Message = "Image uploaded successfully!";
                return(View());
            }

            catch
            {
                //Using this empty string for the View to trigger when an upload fails
                ViewBag.ImageData = "";
                ViewBag.Message   = "There was an error uploading your image :(";
            }
            return(View());
        }