Beispiel #1
0
        public IActionResult UploadMultipleFiles(int ProductId)
        {
            var response = new OperationResponse <ProductImages>();

            try
            {
                if (Request.Form.ContainsKey("ProductId"))
                {
                    ProductId = Convert.ToInt32(Request.Form["ProductId"].ToString());
                }

                if (Request.Form.Files.Count > 0)
                {
                    foreach (var item in Request.Form.Files)
                    {
                        int MaxId = _productImageService.GetMaxProductImageId();

                        var file = item;

                        string OriginalImagePath  = _configuration["ImagePathConfiguration:OriginalImagePath"];
                        string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"];

                        var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var fileExt  = fileName.Remove(0, fileName.LastIndexOf('.'));
                        fileName = (MaxId + 1).ToString() + fileExt;

                        ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName);

                        ProductImages request = new ProductImages();
                        request.ImageName     = fileName;
                        request.ImagePath     = Path.Combine(OriginalImagePath, fileName);
                        request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName);
                        request.Description   = "";
                        request.DisplayOrder  = 1;
                        request.IsDisplay     = true;

                        response.Data = _productImageService.AddProductImages(request);

                        ProductImageProduct request2 = new ProductImageProduct();
                        request2.ProductId      = Convert.ToInt32(ProductId);
                        request2.ProductImageId = response.Data.RowId;
                        _productImageService.AddProductImage_Product(request2);
                    }
                }
                else
                {
                    response.Messages.Add("Please upload Image");
                }
            }
            catch (Exception exception)
            {
                response.State = ResponseState.Error;
                response.Messages.Add(exception.Message);
            }
            return(new JsonResult(response));
        }