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
}
2
  • 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 Commented Oct 20, 2016 at 9:54
  • 1
    Hey, which do you think is the best approach? Commented Dec 11, 2018 at 12:51

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

1 Comment

i was missing enctype: <form method="post" enctype="multipart/form-data">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.