Home > front end >  Uploading and handling .csv file to an ASP.NET Core Web API
Uploading and handling .csv file to an ASP.NET Core Web API

Time:01-13

I'm relatively new to the backend/C# programming but I have a task to deal with it.

I have created a simple CRUD operation for managing books using the ASP.NET Core Web API.

Here's my task:

I need to upload a .csv or Excel related files to an endpoint, validate the file extension and populate the database with the data in the file.

The POST endpoint that I've created does add a new book, but only one at a time. So generally what I'm trying to do here is creating an endpoint for adding multiple books at once using the data in the .csv file.

Any kind of hint or help would be highly appreciated. Thank you.

CodePudding user response:

I maintain a library that might help you with this: Sylvan.AspNetCore.Mvc.Formatters.Csv.

It implements an AspNET formatter for CSV. If you already have an API endpoint that handles a single item, this can make it very easy to add support for loading multiple.

Assuming you have an existing API:

class Book {
  public string Author { get;set;}
  public DateTime PublishDate {get;set;}
  public string ISBN {get;set;}
  // ...
}

class LibraryController {
  [HttpPost]
  public async Task AddBook(Book book) 
  {
    // your existing book insert code

  }
}

After adding the CSV formatter you can add a new endpoint as follows:

[HttpPost]
public async Task AddBooks(IEnumerable<Book> books) {

  foreach(var book in books) {
    await AddBook(book);
  }
}

The package takes care of parsing the incoming CSV data and binding it to Book. This allows a client to optionally send JSON or CSV, determined by the Content-Type request header.

If the data file is expected to be very large, it would be more efficient to load the CSV data into the database with a SqlBulkCopy (or the equivalent for your database system), but you probably shouldn't worry about that unless you run into performance issues.

CodePudding user response:

You can upload multiple files using IFormCollection. Examples can be found here- https://csharp.hotexamples.com/examples/-/IFormCollection/-/php-iformcollection-class-examples.html Then you can read the csv into your class using csvHelper nuget(https://www.nuget.org/packages/CsvHelper/) and save the objects to database. examples are here- https://joshclose.github.io/CsvHelper/examples/reading/get-class-records/

IFormFile for single file, IFormCollection for multiple files

  •  Tags:  
  • Related