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.

7
  • 1
    Since you are in a controller have you tried accessing the this.Request.Form.Files collection instead of getting it as a parameter? Commented Aug 14, 2019 at 7:15
  • what's the path you're uploading to? Commented Aug 14, 2019 at 7:16
  • is this an APIController and are you sending the file via a multipart/form-data post?
    – scgough
    Commented Aug 14, 2019 at 7:25
  • This is an APIController and it is send as an multipart/form-data. In the end it gets uploaded to an Blob storage. Locally it still works. Now checking @BercoviciAdrian solution to use this.Request.Form.Files unstead of getting it as an paramter Commented Aug 14, 2019 at 7:45
  • Can you access the log message with the given traceId? Commented Aug 14, 2019 at 8:07

2 Answers 2

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.

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
}
1
  • 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: stackoverflow.com/questions/76455018/…
    – malthe.w
    Commented Jun 13, 2023 at 8:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.