Example #1
0
        public ActionResult Show(string idString)
        {
            using (var context = new ImgHostContext())
            {
                long id = Base62.FromBase(idString);

                var requestedImage = context.Images.FirstOrDefault(i => i.Id == id);

                //no result? return error page
                if (requestedImage != null) return View(requestedImage);

                return View("Error", new Error { Message = "Sorry, we couldn't find the requested image!"});
            }
        }
Example #2
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file.ContentLength <= 0)
                return View("Error", new Error { Message = "There was a problem uploading your file, please try again later."});

            if (file.ContentLength > 3145728)
                return View("Error", new Error { Message = "Sorry, file size cannot be over 3MB!"});

            //generate unique filename
            var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
            var serverPath = Path.Combine(Server.MapPath("~/uploads"), fileName);

            var img = new HostedImage
            {
                FileName = fileName,
                FullServerPath = serverPath
            };

            try
            {
                using (var context = new ImgHostContext())
                {
                    context.Images.Add(img);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return View("Error", new Error { Message = e.InnerException.Message });
            }

            file.SaveAs(serverPath);

            //pass to /images/encodedname to display the image after upload
            return Redirect(string.Format("~/images/{0}", img.EncodedName));
        }