Should I use IFormFile to Upload Files in ASP.NET Core API

Muhammad Rehan Saeed picture Muhammad Rehan Saeed · Oct 20, 2016 · Viewed 8k times · Source

I can understand using IFormFile to upload files in an MVC web app but what is the correct method of uploading files using ASP.NET Core when writing an API supporting Swagger?

With IFormFile

[HttpPost("{id}/content", Name ="PostZipFile")]
[Consumes("application/zip")]
public Task<IActionResult> PostZipFile(int id, [FromBody] IFormFile zipFile)
{
}

Using Body.Stream

[HttpPost("{id}/content", Name ="PostZipFile")]
[Consumes("application/zip")]
public Task<IActionResult> PostZipFile(int id)
{
    this.Response.Body.Stream
}

Answer

Mahdi Amini picture Mahdi Amini · Jan 28, 2017

Try this :

[HttpPost("{id}/content", Name ="PostZipFile")]
public Task<IActionResult> PostZipFile(int id,IFormFile zipFile)
{
}

And make sure that on the Client Side, the Form Action or Ajax request has: encoding type= "multipart/form-data"