I have a requirement where I need to import csv from a client browser app (built using Angular4) and send that to a web api (built using ASP.net Core 2).
My options for sending csv data are:
Send csv text in request body - e.g we input csv text to textarea input
Send csv file in request - e.g use input type="file"
For option 1, I've used this solution for converting text in a request body to a typed object. Works great for small csv data:
https://damienbod.com/2016/06/17/import-export-csv-in-asp-net-core/
For option 2, I've done something like this before (example in MVC):
View (cshtml) :
<form method="post" asp-action="Upload" asp-controller="Home" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload"/>
</form>
Web Api controller (cs) :
[HttpPost]
public IActionResult Upload(IFormFile file)
{
if (file == null || file.Length == 0)
throw new Exception("file should not be null");
using (var fileStream = new FileStream("path_address", FileMode.Create))
{
await file.CopyTo(fileStream);
}
}
Again, this works well with smaller csv. What I need now is to send very large csv files (few gigabytes in size potentially). And I'd like a solution for ASP.net Core 2 (sent from Angular as mentioned) where I can split the file into chunks and send to the server piece by piece.
Ideally, as each chunk is sent to the server, I'd like to view the contents of the chunk in the code and verify its valid - so converting the chunk to text and validating things like a) it has the expected number of columns, b) each row (which will be mapped to a data type on the server) has the expected type for each field e.g if the object being mapped to has an int, that what's passed isn't a string.
Has anyone used chunking before in ASP.net Core 2?
Thanks for any pointers in advance!