简体   繁体   中英

How to read multipart/form-data?

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  
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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