Reputation: 21
How to read multipart/form-data that is a csv file in asp.net core C# sent through api and save it in local?
public async Task<IActionResult> PostAsync()
{
var file=Request.?? //code to read multipart/form-data
}
Upvotes: 1
Views: 1411
Reputation: 794
You should get the file with a model or a parameter. The file interface for asp.net core is IFormFile
.
Form input on Html side.
<input type="file" name="testFile" />
Then you can get it like this.
[HttpPost]
public async Task<IActionResult> PostAsync([FromForm]IFormFile testFile)
{
var path = CreatePathUsingFileName(testFile.FileName);
await using var fileStream = System.IO.File.Create(path);
await testFile.CopyToAsync(fileStream, CancellationToken.None);
return Ok();
}
Upvotes: 2