Example #1
0
        public async Task <IActionResult> Create([FromBody] CreateProductImgRequest productImgRequest)
        {
            var ProductImage = new ProductImage
            {
                ProductId = productImgRequest.ProductId,
                ImgUrl    = productImgRequest.ImgUrl,
                Ext       = productImgRequest.Ext,
                CreatedAt = DateTime.Now,
            };

            var status = await _productImagesService.CreateProductImageAsync(ProductImage);

            if (status == 1)
            {
                var response = new ProductImgResponse {
                    Id = ProductImage.Id
                };
                return(Ok(response));
            }
            return(NotFound(new ErrorResponse
            {
                message = "Not Found",
                status = NotFound().StatusCode
            }));
        }
Example #2
0
        public async Task <IActionResult> UploadImages([FromRoute] Int64 productId)
        {
            string folderPath = "wwwroot/Resources/Images/ProductImg/";
            bool   exists     = Directory.Exists(folderPath);

            if (!exists)
            {
                Directory.CreateDirectory(folderPath);
            }

            var files      = Request.Form.Files;
            var folderName = Path.Combine(folderPath);
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
            var dbPath     = "";

            if (files.Count > 0)
            {
                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        var fileName = DateTime.Now.Ticks + "_" + ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var fullPath = Path.Combine(pathToSave, fileName);
                        dbPath = Path.Combine(folderName, fileName);

                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }

                        var ProductImage = new ProductImage
                        {
                            ProductId = productId,
                            ImgUrl    = "http://husamalraie-001-site3.gtempurl.com/" + dbPath,
                            //ImgUrl = "https://localhost:44304/" + dbPath,
                            Ext       = Path.GetExtension(file.FileName),
                            CreatedAt = DateTime.Now,
                        };

                        await _productImagesService.CreateProductImageAsync(ProductImage);
                    }
                }

                return(Ok(new { status = Ok().StatusCode, message = "Uploade Successfully" }));
            }
            return(BadRequest());
        }