Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ImageExists(Image.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 2
0
        //method to handle program logic after form is submitted
        public async Task <IActionResult> OnPostAsync()
        {
            if (Request.Form.Files.Count > 0)
            {
                if (!ModelState.IsValid)
                {
                    return(Page());
                }

                //save file to database
                Image.FileName = Upload.FileName;
                Image.Title    = "(No Title)";
                _context.Image.Add(Image);
                await _context.SaveChangesAsync();

                var file = Path.Combine(_environment.WebRootPath, "Uploads", Upload.FileName);

                //save file to specified file system
                using (var fileStream = new FileStream(file, FileMode.Create))
                {
                    await Upload.CopyToAsync(fileStream);

                    ImagePath = Url.Content("~/Uploads/" + Upload.FileName);
                    return(RedirectToPage("./Index"));
                }
            }
            else
            {
                Message = "incomplete";
                return(Page());
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Image = await _context.Image.FindAsync(id);

            //delete file from file system
            if (Image != null)
            {
                _context.Image.Remove(Image);
                await _context.SaveChangesAsync();

                webRootPath = _environment.WebRootPath;
                ImagePath   = webRootPath + "/Uploads/" + Image.FileName;
                System.IO.File.Delete(ImagePath);
            }

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