public async Task <IActionResult> UploadFile(IFormFile file) { var model = new FileOutputModel(); if (file == null || file.Length == 0 || file.ContentType.ToLower() != "text/plain") { model.InvalidFile = true; return(View("Index", model)); } var fileExt = Path.GetExtension(file.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\uploads\output.txt"); model.Name = Path.GetFileName(file.FileName); model.Path = file.FileName; model.Url = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/uploads/output.txt"; //read file var fileStream = await file.GetFileStream(); fileStream.Position = 0; List <CheckoutModel> checkoutModel = new List <CheckoutModel>(); using (StreamReader reader = new StreamReader(fileStream)) { string line; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } var checkout = new CheckoutModel(); checkout.OwnedAmount = decimal.Parse(line.Split(",")[0].Trim()); checkout.PaidAmount = decimal.Parse(line.Split(",")[1].Trim()); checkoutModel.Add(checkout); } } model.CheckoutModel = checkoutModel; var calculatedModel = _checkoutService.Calculate(checkoutModel); //write to the output file using (StreamWriter writetext = new StreamWriter(path, false)) { foreach (var line in calculatedModel) { writetext.WriteLine(line.ChangeString); } } return(View("Index", model)); }