4

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
}
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • 1
    It still applies to WebApi-esque controllers. In ASP.NET Core both are unified in a single API. You just need to send the ajax post request with `multipart/form-data` encoding type – Tseng Oct 20 '16 at 09:54
  • 1
    Hey, which do you think is the best approach? – Neel Dec 11 '18 at 12:51

1 Answers1

2

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"