Exemple #1
0
        public async Task <IActionResult> PutPrdImage(int id, PrdImage prdImage)
        {
            if (id != prdImage.Id)
            {
                return(BadRequest());
            }

            _context.Entry(prdImage).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PrdImageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #2
0
        //public async Task<ActionResult<PrdImage>> PostPrdImage(PrdImage prdImage)
        //{
        //    _context.PrdImages.Add(prdImage);
        //    await _context.SaveChangesAsync();

        //    return CreatedAtAction("GetPrdImage", new { id = prdImage.Id }, prdImage);
        //}
        //public async Task<ActionResult<PrdImage>> PostPrdImage(PrdImage prdImage)
        public IActionResult PostPrdImage()
        {
            try
            {
                var    httpreq  = HttpContext.Request.Body;
                var    pid      = Request.Form["Pid"].ToString();
                var    file     = Request.Form.Files[0];
                string rootPtah = Path.Combine(this._env.WebRootPath, "ProductImage");

                if (!Directory.Exists(rootPtah))
                {
                    Directory.CreateDirectory(rootPtah);
                }
                if (file.Length > 0)
                {
                    string fileName       = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    string ext            = Path.GetExtension(fileName);
                    string filewithoutext = Path.GetFileNameWithoutExtension(fileName);
                    string filepath       = Path.Combine(rootPtah, (filewithoutext + "_" + pid + ext));
                    using (var stream = new FileStream(filepath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }

                    string imagePath = "/ProductImage/" + filewithoutext + "_" + pid + ext;
                    var    prd       = new PrdImage
                    {
                        ProductId = Int32.Parse(pid),

                        OriginalImagePath = imagePath,
                    };
                    _context.PrdImages.Add(prd);
                    if (_context.SaveChanges() > 0)
                    {
                        return(Created("api/PrdImages", prd));
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            // return Ok(new { resul = "Successfuly created" });
            return(BadRequest());
        }