Example #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.

        //public async Task<IActionResult> OnPostAsync()
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return Page();
        //    }
        //    if (Image != null)
        //    {
        //        var fileName = $"{Food.FoodId}" + Path.GetExtension(Image.FileName);
        //        Food.Image = fileName;
        //        var path = Path.Combine(_environment.WebRootPath, "Images", fileName);
        //        using (var fStream = new FileStream(path, FileMode.Create))
        //        {
        //            await Image.CopyToAsync(fStream);
        //        }
        //    }
        //}



        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            if (Image != null)
            {
                var fileName = $"{Food.FoodId}" + Path.GetExtension(Image.FileName);
                Food.Image = fileName;
                var path = Path.Combine(_environment.WebRootPath, "Images", fileName);
                using (var fStream = new FileStream(path, FileMode.Create))
                {
                    await Image.CopyToAsync(fStream);
                }
            }

            _context.Attach(Food).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FoodExists(Food.FoodId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Example #2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            string path = "";
            // предыдущее изображение
            string previousImage = String.IsNullOrEmpty(Flower.FlowerImage)
            ? ""
            : Flower.FlowerImage;

            if (image != null)
            {
                // новый файл изображения
                Flower.FlowerImage = Flower.FlowerId + Path.GetExtension(image.FileName);
                // путь для нового файла изображения
                path = Path.Combine(_environment.WebRootPath, "images", Flower.FlowerImage);
            }
            _context.Attach(Flower).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();

                if (image != null)
                {
                    // если было предыдущее изображение
                    if (!String.IsNullOrEmpty(previousImage))
                    {
                        // если файл существует
                        var fileInfo = _environment.WebRootFileProvider
                                       .GetFileInfo("/Images/Flowers/" + previousImage);
                        if (fileInfo.Exists)
                        {
                            var oldPath = Path.Combine(_environment.WebRootPath,
                                                       "images", previousImage);
                            // удалить предыдущее изображение
                            System.IO.File.Delete(oldPath);
                        }
                    }
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        // сохранить новое изображение
                        await image.CopyToAsync(stream);
                    };
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FlowerExists(Flower.FlowerId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }