.net core 2.1"POST"使用邮递员的IFormFile

编程入门 行业动态 更新时间:2024-10-17 17:16:27
本文介绍了 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

本文发布于:2023-11-14 09:46:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮递员   core   net   IFormFile   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!