简体   繁体   English

.net 核心 API 文件上传不适用于 Azure

[英].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.当我们将它放在 Azure 上时,我们会得到以下响应。

{"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.看起来我们不允许在 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.该项目是一个 .NET Core 2.2 MVC 项目,它在标准 Azure Web 应用程序上运行。

When wanting to use Forms that have files attached to them (eg multipart requests) we can access the request's files using:当想要使用附加了文件的Forms (例如multipart请求)时,我们可以使用以下方法访问请求的文件:

Request.Form.Files which represents the file collection of the incoming form. Request.Form.Files表示传入表单的文件集合。

The desired file will be read as a stream using the OpenReadStream method and then deserialized.所需的文件将使用OpenReadStream方法作为流读取,然后反序列化。

The fix was to change it to Request.Form.Files as Bercovici Adrian suggested.修复方法是按照 Bercovici Adrian 的建议将其更改为 Request.Form.Files。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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