public IActionResult Create(UploadCreateViewModel model) { if (ModelState.IsValid) { string uniqueFileName = null; if (model.Project != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "projects"); uniqueFileName = model.Project.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Project.CopyTo(fileStream); } } Upload newUpload = new Upload { Name = model.Name, Description = model.Description, FileExtention = Path.GetExtension(model.Name), Size = (model.Length / 1024),// For get size in KB Icon = GetIconPath(Path.GetExtension(model.Name)), UploadPath = uniqueFileName }; _uploadRepository.Add(newUpload); return(RedirectToAction("details", new { id = newUpload.Id })); } return(View()); }
// public async Task<IActionResult> OnPostUploadAsync(List<IFormFile> files) // { // long size = files.Sum(f => f.Length); // foreach (var formFile in files) // { // if (formFile.Length > 0) // { // var filePath = Path.GetTempFileName(); // using (var stream = System.IO.File.Create(filePath)) // { // await formFile.CopyToAsync(stream); // } // } // } // // Process uploaded files // // Don't rely on or trust the FileName property without validation. // return Ok(new { count = files.Count, size, filePath }); // } // public IActionResult Get(IFileProvider fileProvider) // { // IDirectoryContents contents = fileProvider.GetDirectoryContents("wwwroot/updates"); // var lastUpdate = // contents.ToList() // .OrderByDescending(f => f.LastModified) // .FirstOrDefault(); // return Ok(lastUpdate?.Name); // } private string ProcessUploadedFile(UploadCreateViewModel model) { string uniqueFileName = null; if (model.Project != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "projects"); uniqueFileName = model.Project.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Project.CopyTo(fileStream); } } return(uniqueFileName); }