.net 核心 2.1 “POST"使用 Postman 的 IFormFile - 应用程序在未读取整个请求正文的情况下完成 [英] .net core 2.1 "POST" an IFormFile using Postman - the application completed without reading the entire request body

查看:63
本文介绍了.net 核心 2.1 “POST"使用 Postman 的 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();
    }

这是我的邮递员电话:

这个调用永远不会结束,因为 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.

另外,不要忘记在 postman 中将请求的标头设置为: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 核心 2.1 “POST"使用 Postman 的 IFormFile - 应用程序在未读取整个请求正文的情况下完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆