C. Molendijk
C. Molendijk

Reputation: 2834

.net core API fileupload not working on Azure

We got the following code in our controller:

[HttpPost("v1/item/{id}/images")]
public async Task<ActionResult> UploadImage([FromRoute]string Id, [FromForm]IFormFile file)
{
    //Upload image logic
}

Local this code works like we expect it to work. When we put this on Azure we get the following response.

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"Bad Request","status":400,"traceId":"|1587f1cc093cd640a1ece0a37a6b33b5.d408b19_"}

It looks like we are not allowed to upload an file this way on Azure. But cannot find any way to make this work.

The project is an .NET Core 2.2 MVC project and it runs on an standaard Azure Web App.

Upvotes: 1

Views: 420

Answers (2)

Bercovici Adrian
Bercovici Adrian

Reputation: 9370

When wanting to use Forms that have files attached to them (e.g multipart requests) we can access the request's files using:

Request.Form.Files which represents the file collection of the incoming form.

The desired file will be read as a stream using the OpenReadStream method and then deserialized.

Upvotes: 1

C. Molendijk
C. Molendijk

Reputation: 2834

The fix was to change it to Request.Form.Files as Bercovici Adrian suggested. So the code is now:

[HttpPost("v1/item/{id}/images")]
public async Task<ActionResult> UploadImage([FromRoute]string Id)
{
    IFormFile file = Request.Form.Files[0]
    //Upload image logic
}

Upvotes: 0

Related Questions