简体   繁体   English

上传和处理.csv文件到ASP.NET内核Web ZDB974238714CA8DE634A7CE1D08

[英]Uploading and handling .csv file to an ASP.NET Core Web API

I'm relatively new to the backend/C# programming but I have a task to deal with it.我对后端/C# 编程比较陌生,但我有一个任务来处理它。

I have created a simple CRUD operation for managing books using the ASP.NET Core Web API.我使用 ASP.NET 核心 Web API 创建了一个简单的 CRUD 操作来管理书籍。

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.我需要将.csv或 Excel 相关文件上传到端点,验证文件扩展名并使用文件中的数据填充数据库。

The POST endpoint that I've created does add a new book, but only one at a time.我创建的POST端点确实添加了一本新书,但一次只能添加一本。 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.所以通常我在这里要做的是创建一个端点,使用.csv文件中的数据一次添加多本书。

Any kind of hint or help would be highly appreciated.任何形式的提示或帮助将不胜感激。 Thank you.谢谢你。

I maintain a library that might help you with this: Sylvan.AspNetCore.Mvc.Formatters.Csv .我维护了一个可以帮助您解决此问题的库: Sylvan.AspNetCore.Mvc.Formatters.Csv

It implements an AspNET formatter for CSV.它为 CSV 实现了一个 AspNET 格式化程序。 If you already have an API endpoint that handles a single item, this can make it very easy to add support for loading multiple.如果您已经有一个处理单个项目的 API 端点,则可以很容易地添加对加载多个项目的支持。

Assuming you have an existing API:假设您有一个现有的 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:添加 CSV 格式化程序后,您可以添加新端点,如下所示:

[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. package 负责解析传入的 CSV 数据并将其绑定到 Book。 This allows a client to optionally send JSON or CSV, determined by the Content-Type request header.这允许客户端有选择地发送 JSON 或 CSV,由Content-Type请求 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.如果预计数据文件非常大,使用SqlBulkCopy (或数据库系统的等效项)将 CSV 数据加载到数据库中会更有效,但除非遇到性能问题。

You can upload multiple files using IFormCollection.您可以使用 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 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/ )并将对象保存到数据库。 examples are here- https://joshclose.github.io/CsvHelper/examples/reading/get-class-records/ I can prepare a working sample if you want, but need sometime for that示例在这里 - https://joshclose.github.io/CsvHelper/examples/reading/get-class-records/如果你愿意,我可以准备一个工作样本,但需要一些时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM