public async Task <IActionResult> Create([Bind("Id,Name,DateOfCreate,DateOfChange,Path,Size,UserId")] Folder folder) { if (ModelState.IsValid) { _context.Add(folder); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(folder)); }
public async Task <IActionResult> Create(/*[Bind("Id,Name,Path,Size,FolderId")] Models.File file,*/ IFormFile content) { var file = new Models.File(); if (ModelState.IsValid) { if (content != null) { if (content.Length > 0) { var id = User.FindFirstValue(ClaimTypes.NameIdentifier); var user = _context.Users.FirstOrDefault(u => u.Id == id); if (content.Length + user.UseStorage <= user.Storage) { int?folderId; using (var memoryStream = new MemoryStream()) { file.Name = content.FileName; //file.Size = (int)content.Length; file.Extension = Path.GetExtension(content.FileName).Remove(0, 1); var fileType = FileType(ref file); folderId = _context.Folder.Where(f => f.UserId == id).FirstOrDefault(f => f.Name == fileType).Id; file.FolderId = folderId; _context.Folder.Where(f => f.UserId == id).FirstOrDefault(f => f.Name == fileType).DateOfChange = DateTime.Now; await content.CopyToAsync(memoryStream); file.ExtualyFile = memoryStream.ToArray(); _context.Folder.Where(f => f.UserId == id).FirstOrDefault(f => f.Name == fileType).Size += file.ExtualyFile.Length; } _context.Add(file); await _context.SaveChangesAsync(); return(LocalRedirect("~/Folders/Details/" + folderId)); } else { ViewBag.Modal = "size"; return(View()); // return PartialView("FilesOutOfMemory"); } } } else { ViewBag.Modal = "null"; return(View()); } } ViewData["FolderId"] = new SelectList(_context.Set <Folder>(), "Id", "Id", file.FolderId); return(View(file)); }