1

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.

C. Molendijk
  • 2,614
  • 3
  • 26
  • 35

2 Answers2

1

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.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
0

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
}
C. Molendijk
  • 2,614
  • 3
  • 26
  • 35
  • I have been Googling for 3 days and this is the only case that is very similar to my problem and unfortunately this did not work for me. In my case, I'm trying to upload an image to an S3 bucket via my Azure web app API. Everything works through localhost + endpoint, but I cannot seem to upload a file through my actual domain + endpoint. I also submitted my issue as a question which can be viewed here: https://stackoverflow.com/questions/76455018/unable-to-upload-an-image-file-to-my-aws-s3-bucket-via-my-azure-rest-api – malthe.w Jun 13 '23 at 08:34