简体   繁体   中英

.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.

When wanting to use Forms that have files attached to them (eg 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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM