static int countapi = 0; [HttpPost] [Authorize] public ActionResult Upload(string path, HttpPostedFileBase file) { try { countapi++; System.Diagnostics.Debug.WriteLine("Upload API called: " + countapi + " times"); var supportedExtensions = SupportedExtensions.Split(','); var fileExtension = Path.GetExtension(file.FileName).ToLower(); if (!supportedExtensions.Contains(fileExtension)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Unsupported file type."); } var directoryPath = Path.Combine(BasePath, path); var fileName = file.FileName; var fullPath = Path.Combine(directoryPath, fileName); if (System.IO.File.Exists(fullPath)) { int count = 1; string originalFileName = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(file.FileName); while (System.IO.File.Exists(fullPath)) { fileName = $"{originalFileName} ({count++}){extension}"; fullPath = Path.Combine(Path.Combine(BasePath, path), fileName); } } if (!Directory.Exists(Path.Combine(BasePath, path))) { Directory.CreateDirectory(Path.Combine(BasePath, path)); } file.SaveAs(fullPath); var entry = new FileManagerEntry { Name = Path.GetFileNameWithoutExtension(file.FileName), Size = file.ContentLength, Extension = Path.GetExtension(file.FileName), Path = fullPath, IsDirectory = false, Created = DateTime.Now, CreatedUtc = DateTime.UtcNow, Modified = DateTime.Now, ModifiedUtc = DateTime.UtcNow }; return Json(entry); } catch (Exception ex) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.InternalServerError, ex.Message); } }