本文介绍了.net core 2.1"POST"使用邮递员的IFormFile-应用程序完成,而无需读取整个请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dotnet核心WebAPI 2.1,但是找不到将图像发送到正文中的方法.

I'm working on a dotnet core WebAPI 2.1 and I can't find a way of sending to into the Body an image.

控制器看起来像这样:

    [HttpPost("api/image")]
    public IActionResult Post([FromBody]IFormFile file)
    {
        var filePath = Path.GetTempFileName();
        if (file.Length > 0)
        {
            return Ok();
        }
        return BadRequest();
    }

这是我的邮递员电话:

And this is my Postman call:

由于Kestrel失败,此呼叫永远不会结束

This call is never finishing as the Kestrel is failing

我已经尝试使用Consumes

[Consumes("application/json", "application/json-patch+json", "multipart/from-data")]

在邮递员中,我还将Content-Type设置为multipart/form-data

Also in postman, I have set Content-Type to multipart/form-data

推荐答案

尝试这样做.在邮递员中使用您现在使用的相同请求.这只是粗略的样板方法,但是您可以理解.

Try doing it like this. Use the same request in postman you are using now. This is just crude boilerplate method but you get the idea.

此外,别忘了在邮递员中将请求的标头设置为:Content-Type:multipart/form-data

Also, dont forget to set headers of your request in postman to: Content-Type: multipart/form-data

[HttpPost]
[Route("api/image")]
public async Task<IHttpActionResult> InsertNewMerchant()
{
        // your form data is here
             var formData = HttpContext.Current.Request.Form;
             HttpFileCollection files = HttpContext.Current.Request.Files;


            if (files.Count == 1)
            {
            // this is the file you need 
                var image = files[0];
                    // do something with the file
            }
    return StatusCode(System.Net.HttpStatusCode.Created);
}

这篇关于.net core 2.1"POST"使用邮递员的IFormFile-应用程序完成,而无需读取整个请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 23:29