0

I am developing a controller in ASP.NET Core 6.0 and I want to implement a post consumer that accept binary images.

If I write the signature in this way:

[HttpPost("image/{pv}/{articolo}"), 
 Consumes("image/jpeg", new string[] { "image/jpg", "image/png", "image/tiff", "image/tif" }),
 SwaggerResponse(StatusCodes.Status200OK), 
 SwaggerResponse(StatusCodes.Status400BadRequest, "nel caso in cui PV o articolo non esistono")]
public async Task<IActionResult> postImage(string pv, string articolo, [FromBody] Stream fileStream)
{ 
    /* use fileStream to save into disk*/ 
}

The swagger works great: screenshot of swagger But If I try it returns error 415 "Unsupported Media Type"

I tried removing [FromBody], the only way to not get 415 is remove the parameter and use Request.Body, but I need the swagger as documentation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Perry
  • 1,113
  • 2
  • 10
  • 22
  • I don't think `Stream` is the correct type to use here, try `[FromBody] IFormFile formFile` instead. – phuzi Dec 06 '22 at 16:48
  • There's a similar question here: https://stackoverflow.com/questions/51021182/httppostedfilebase-in-asp-net-core-2-0 which was asking about the ASP.NET Core equivalent to `HttpPostedFileBase` that was the correct type for .NET Framework. – phuzi Dec 06 '22 at 16:49
  • `IFormFile` it is for file inside a form, it is not my case. – Perry Dec 09 '22 at 07:38
  • I already have a webservice that takes Images using post with `content-type: image/jpeg` it is not possible do it in .net core? – Perry Dec 09 '22 at 16:55

0 Answers0