public JsonResult UploadImageDecription(HttpPostedFileBase file)
        {
            string link     = string.Empty;
            string filename = Guid.NewGuid().ToString() + ".jpg";
            string image    = Server.MapPath(Constants.ProductDescriptionPath) + filename;

            try
            {
                // The Complete method commits the transaction. If an exception has been thrown,
                // Complete is not  called and the transaction is rolled back.
                Bitmap imgCropped = new Bitmap(file.InputStream);
                var    saveImage  = ImageWorker.CreateImage(imgCropped, 450, 450);
                if (saveImage == null)
                {
                    throw new Exception("Error save image");
                }
                saveImage.Save(image, ImageFormat.Jpeg);
                link = Url.Content(Constants.ProductDescriptionPath) + filename;
                ProductDescriptionImage pImage = new ProductDescriptionImage()
                {
                    Name = filename
                };
                _context.ProductDescriptionImages.Add(pImage);
                _context.SaveChanges();
            }
            catch (Exception)
            {
                if (System.IO.File.Exists(image))
                {
                    System.IO.File.Delete(image);
                }
            }

            return(Json(new { link, filename }));
        }
Пример #2
0
        public JsonResult UploadImageDescription(HttpPostedFileBase file)
        {
            //string pathServer = ConfigurationManager.AppSettings["UserImagePath"];
            //string path = Server.MapPath(pathServer);

            //var image = Guid.NewGuid().ToString() + ".jpg";
            //string savepath = path + image;
            //Bitmap imageBig = ImageWorker.CreateImage(file, 1100, 1200);
            //imageBig.Save(savepath, ImageFormat.Jpeg);

            string link     = string.Empty;
            var    filename = Guid.NewGuid().ToString() + ".jpg";
            string image    = Server.MapPath(Constants.ProductDescriptionPath) + filename;

            try
            {
                using (Bitmap btn = new Bitmap(file.InputStream))
                {
                    var saveImage = ImageWorker.CreateImage(btn, 450, 450);
                    if (saveImage != null)
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            var pdImage = new ProductDescriptionImage
                            {
                                Name = filename
                            };
                            _context.ProductDescriptionImages.Add(pdImage);
                            _context.SaveChanges();
                            saveImage.Save(image, ImageFormat.Jpeg);
                            link = Url.Content(Constants.ProductDescriptionPath) + filename;
                            scope.Complete();
                        }
                    }
                }
            }
            catch
            {
                if (System.IO.File.Exists(image))
                {
                    System.IO.File.Delete(image);
                }
                link = string.Empty;
            }

            return(Json(new { link, filename }));
        }
Пример #3
0
        public JsonResult UploadImageDecription(HttpPostedFileBase file)
        {
            string link     = string.Empty;
            string filename = Guid.NewGuid().ToString() + ".jpg";
            string image    = Server.MapPath(Constants.ProductDescriptionPath) +
                              filename;

            try
            {
                using (Bitmap bmp = new Bitmap(file.InputStream))
                {
                    var saveImage = ImageWorker.CreateImage(bmp, 450, 450);
                    if (saveImage != null)
                    {
                        saveImage.Save(image, ImageFormat.Jpeg);
                        link = Url.Content(Constants.ProductDescriptionPath) +
                               filename;
                        var pdImage = new ProductDescriptionImage
                        {
                            Name = filename
                        };
                        _context.ProductDescriptionImages.Add(pdImage);
                        _context.SaveChanges();
                    }
                }
                string path = Url.Content(Constants.ProductDescriptionPath);
            }
            catch (Exception)
            {
                if (System.IO.File.Exists(image))
                {
                    System.IO.File.Delete(image);
                }
            }
            return(Json(new { link, filename }));
        }