コード例 #1
0
        public ActionResult New()
        {
            var viewModel = new ProductPhotoFormViewModel()
            {
                Id        = 0,
                ProductId = (int)Session["ProductId"]
            };

            return(View("ProductPhotoForm", viewModel));
        }
コード例 #2
0
        public ActionResult Create(ProductPhotoFormViewModel viewModel, HttpPostedFileBase PhotoUrl)
        {
            if (PhotoUrl == null)
            {
                ModelState.AddModelError("PhotoUrl", "PhotoUrl can't be blank");
                return(View("ProductPhotoForm", viewModel));
            }

            if (PhotoUrl.ContentLength > 2000000)
            {
                ModelState.AddModelError("PhotoUrl", "PhotoUrl can't be more than 2Mb Size.");
                return(View("ProductPhotoForm", viewModel));
            }

            string basePath       = Server.MapPath("~");
            string folderPath     = "Upload\\";
            string folderFullPath = basePath + "" + folderPath;
            string fillName       =
                string.Format(DateTime.Now.Day + "" + DateTime.Now.Month + "" + DateTime.Now.Year + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "" + Path.GetExtension(PhotoUrl.FileName));

            if (!Directory.Exists(folderFullPath))
            {
                Directory.CreateDirectory(folderFullPath);
            }

            Bitmap bmpPostedImage = new System.Drawing.Bitmap(PhotoUrl.InputStream);
            var    image          = ScaleImage(bmpPostedImage, 320, null);

            //PhotoUrl.SaveAs(Path.Combine(folderFullPath, fillName));
            image.Save(Path.Combine(folderFullPath, fillName));
            viewModel.PhotoUrl = folderPath + "" + fillName;

            Bitmap bmpPostedThumbImage = new System.Drawing.Bitmap(PhotoUrl.InputStream);
            var    thumbImage          = ScaleImage(bmpPostedThumbImage, 125, null);

            thumbImage.Save(Path.Combine(folderFullPath, "thumb_" + fillName));
            viewModel.PhotoThumbUrl = folderPath + "thumb_" + fillName;

            var photo = new Photo()
            {
                PhotoUrl      = viewModel.PhotoUrl,
                PhotoThumbUrl = viewModel.PhotoThumbUrl,
                ProductId     = viewModel.ProductId,
                IsActive      = true,
            };

            _context.Photos.Add(photo);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Photos"));
        }