示例#1
0
        public void AddUpload(string imageBytes, int productId, bool isEditing)
        {
            try
            {
                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", productId.ToString());
                if (isEditing)
                {
                    var currentThumbnail = _uploadRepository.GetProductThumbnail(productId);
                    if (currentThumbnail != null)
                    {
                        currentThumbnail.IsDeleted = true;
                        _uploadRepository.EditUpload(currentThumbnail);
                        _uploadRepository.SaveChanges();
                        System.IO.File.Delete(System.IO.Path.Combine(path, "thumbnail.png"));
                    }
                }

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image     = Convert.FromBase64String(imageBytes);
                var    imagePath = System.IO.Path.Combine(path, "thumbnail.png");
                System.IO.File.WriteAllBytes(imagePath, image);
                var userId = _userManager.GetUserId(User);
                var upload = new Upload()
                {
                    Path            = "~/uploads/products/" + productId + "/thumbnail.png",
                    FileName        = "thumbnail.png",
                    ProductId       = productId,
                    CreatedOnDate   = DateTime.Now,
                    CreatedByUserId = userId,
                    IsThumbnail     = true,
                    IsCarousel      = false,
                    IsDeleted       = false
                };
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }
示例#2
0
        public IActionResult OnPostAddUpload(string imageBytes)
        {
            try
            {
                var upload = new Upload();
                upload.IsCarousel      = true;
                upload.IsDeleted       = false;
                upload.IsThumbnail     = false;
                upload.CreatedOnDate   = DateTime.Now;
                upload.CreatedByUserId = _userManager.GetUserId(User);
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();

                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "carousels");

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                var fileName = "Banner" + upload.Id + ".png";

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image = Convert.FromBase64String(imageBytes);

                var imagePath = System.IO.Path.Combine(path, fileName);
                System.IO.File.WriteAllBytes(imagePath, image);

                upload.Path     = "~/uploads/carousels/" + fileName;
                upload.FileName = fileName;

                _uploadRepository.EditUpload(upload);
                _uploadRepository.SaveChanges();

                return(RedirectToPage("/Panel/Carousel"));
            }
            catch (Exception ex)
            {
                return(RedirectToPage("/Panel/Carousel"));
            }
        }
示例#3
0
 public async Task <ActionResult> AddUpload(Upload upload)
 {
     return(Ok(await _uploadRepository.AddUpload(upload)));
 }