//[Authorize(AuthenticationSchemes = "Bearer")] public IActionResult UploadfileInGroup(IFormFile file, [FromForm] int idfolder, int idgroup) { var f = new File(); // var file = Request.Form.Files[0]; // var FID = Request.Form.Files[1].ContentDisposition; var folderName = Path.Combine("ressources"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); var dbPath = Path.Combine(folderName, fileName); var serverpath = Path.Combine(folderName, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } f.FileName = fileName; f.FilePath = serverpath; f.FileSize = file.Length; f.FileVersion = 1; // var userIdClaim = HttpContext.User.Claims.Where(x => x.Type == "userId").SingleOrDefault().Value; // var user = _context.Users.Where(u => u.Id == userIdClaim).FirstOrDefault(); //var idfolder = Request.Form.files; var folder = _context.Folder.Where(f => f.FolderId == idfolder && f.FolderGroup.GroupId == idgroup).FirstOrDefault(); f.FileFolder = folder; // f.FileOwner = user; f.UploadDate = DateTime.Now; _context.Add(f); _context.SaveChanges(); return(Ok(new { dbPath })); } else { return(BadRequest()); } }
public IActionResult Upload(IFormFile file) { { File f = new File(); var filePath = Path.Combine(@"ressources", file.FileName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var dbPath = Path.Combine(fileName); var serverpath = Path.Combine(fileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(fileStream); } f.FileName = fileName; f.FilePath = serverpath; f.FileSize = file.Length; f.FileVersion = 1; // var userIdClaim = HttpContext.User.Claims.Where(x => x.Type == "UserId").SingleOrDefault().Value; var user = _context.Users.Where(u => u.Id == "UserId").FirstOrDefault(); // var folder = _context.Folder.FirstOrDefault(); // folder.ElementNumber = folder.ElementNumber += 1; // folder.FolderSize = folder.FolderSize + file.Length; // f.FileFolder = folder; f.FileOwner = user; f.UploadDate = DateTime.Now.Date; f.FileOwner = null; f.FileFolder = null; f.FileStatut = 0; _context.Add(f); _context.SaveChanges(); return(Ok(new { dbPath })); } else { return(BadRequest()); } } }