static void Main(string[] args) { var active = true; Console.WriteLine("Please specify the file path for the data.txt file: "); while (active) { var userInput = Console.ReadLine(); if (userInput.Equals("Done")) { active = false; } else { var filename = Path.GetFileName(userInput); if (filename.ToUpper().Equals("DATA.TXT")) { try { var employees = ConvertCSVToEmployee.ConvertCSVFileTOEmployees(userInput); using (var db = new ConnectionString()) { foreach (Employee employee in employees) { db.Add(employee); } db.SaveChanges(); } Console.WriteLine("Employees have been added successfully!"); Console.WriteLine("Please specify the file path for the next data.txt file or enter 'Done' to exit: "); } catch (Exception) { Console.WriteLine("Unable to to process file, please try again!"); } } else { Console.WriteLine("Unable to to locate file, please try again!"); } } } }
public async Task <IActionResult> Post([FromForm(Name = "employeeFile")] IFormFile employeeFile) { string employeeDocumentFilePath = Environment.CurrentDirectory; try { //file checked for null value on form submit employeeDocumentFilePath = Path.Combine(employeeDocumentFilePath, "employeeFile.txt"); using (var stream = System.IO.File.Create(employeeDocumentFilePath)) { await employeeFile.CopyToAsync(stream); } } catch (Exception) { //Delete file if exists if (System.IO.File.Exists(employeeDocumentFilePath)) { System.IO.File.Delete(employeeDocumentFilePath); } return(BadRequest(new { message = "Unable to process file, please try again." })); } try { var employees = ConvertCSVToEmployee.ConvertCSVFileTOEmployees(employeeDocumentFilePath); foreach (Employee employee in employees) { _connectionString.Add(employee); } _connectionString.SaveChanges(); } catch (Exception) { return(BadRequest(new { message = "An error occured when processing employess file." })); } return(Ok()); }