Ejemplo n.º 1
0
        // To protect from overposting attacks, please 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(Tehnika.Images)
                ? ""
                : Tehnika.Images;

            if (image != null)
            {
                Tehnika.Images = Tehnika.TehnikaId + Path.GetExtension(image.FileName);
                path           = Path.Combine(_environment.WebRootPath, "images", Tehnika.Images);
            }

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

            try
            {
                await _context.SaveChangesAsync();

                if (image != null)
                {
                    if (!String.IsNullOrEmpty(previousImage))
                    {
                        var fileInfo = _environment.WebRootFileProvider
                                       .GetFileInfo("/images/" + 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 (!TehnikaExists(Tehnika.TehnikaId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 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());
            }
            if (Image != null)
            {
                var fileName = $"{Dish.DishId}" +
                               Path.GetExtension(Image.FileName);
                Dish.Image = fileName;
                var path = Path.Combine(_environment.WebRootPath, "Images",
                                        fileName);
                using (var fStream = new FileStream(path, FileMode.Create))
                {
                    await Image.CopyToAsync(fStream);
                }
            }
            _context.Attach(Dish).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DishExists(Dish.DishId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

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