How to write Api To Upload A text file and Create a datatable
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Apr 13, 2023, 5:18 AM EDT
Try this
[HttpPost("upload")] public IActionResult Upload(IFormFile file) { // TODO: Implement file upload logic using (var streamReader = new StreamReader(file.OpenReadStream())) { var dataTable = new DataTable(); var headers = streamReader.ReadLine()?.Split(','); if (headers != null) { foreach (var header in headers) { dataTable.Columns.Add(header); } } while (!streamReader.EndOfStream) { var values = streamReader.ReadLine()?.Split(','); if (values != null) { dataTable.Rows.Add(values); } } // TODO: Do something with the datatable } return Ok(); }