コード例 #1
0
        public async Task <IActionResult> Delete()
        {
            foreach (var key in Request.Form.Keys)
            {
                int Id = 0;
                Int32.TryParse(key, out Id);

                if (Id == 0)
                {
                    break;
                }

                var file = await _context.FindAsync <Files>(Id);

                if (file == null)
                {
                    return(NotFound());
                }

                var      path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Files", file.FileName);
                FileInfo info = new FileInfo(path);
                try
                {
                    _context.Files.Remove(file);
                    info.Delete();
                    await _context.SaveChangesAsync();
                }
                catch (Exception)
                {
                    return(View("../Shared/Error"));
                }
            }

            return(RedirectToAction("Index", "Image"));
        }
コード例 #2
0
        /*
         * [HttpGet]
         * public async Task<IActionResult> LoadAllFiles()
         * {
         *  if (!HttpContext.Session.Keys.Contains<string>("Authenticated"))
         *  {
         *      return View("Unauthorized");
         *  }
         *
         *  AllFilesViewModel model = new AllFilesViewModel
         *  {
         *      Images = await _context.Images.ToListAsync<Images>(),
         *      Files = await _context.Files.ToListAsync<Files>(),
         *      Categories = await _context.FileCategories.ToListAsync<FileCategories>(),
         *      FileFolders = await _context.Folders.ToListAsync<Folders>()
         *  };
         *
         *  model.Categories = model.Categories.OrderBy<FileCategories, int>(i => i.CategoryId);
         *  model.Images = model.Images.OrderByDescending<Images, DateTime>(t => t.UploadedDate);
         *  model.Files = model.Files.OrderByDescending<Files, DateTime>(t => t.UploadedDate);
         *
         *  return View("AllFiles", model);
         * } */

        public async Task <IActionResult> Delete(int?Id)
        {
            if (!HttpContext.Session.Keys.Contains <string>("Authenticated"))
            {
                return(View("Unauthorized"));
            }

            if (Id == null)
            {
                return(NotFound());
            }
            var img = await _context.FindAsync <Images>(Id);

            if (img == null)
            {
                return(NotFound());
            }
            FileCategories category = await _context.FileCategories.FirstAsync(c => c.CategoryId == img.CategoryId);

            var path     = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images", category.CategoryLabel, img.ImgFileName);
            var fileInfo = new FileInfo(path);

            try
            {
                _context.Images.Remove(img);
                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(View("../Shared/Error"));
            }

            return(RedirectToAction("Index"));
        }