Post request with image in body .NET Framwork



我需要发布请求api与图像在身体。Swagger像这样展示

和我确实在邮差先请求。邮差身体

邮差头

现在我需要在c#中做这个。

我已经创建了一个void

public void addImage(byte[] byteImage,string filename)
{
try
{
PricerPublicAPIRest api = getAPI();
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
content.Add(new ByteArrayContent(byteImage,0,byteImage.Length), name: "image", fileName: filename);
content.Add(new StringContent("image/png"), "Content-Type");
var unlinkResult = api.SendRequest($"{BASE_URL}file/v1/image", content, new HttpMethod("POST"));
}
}

catch (Exception ex)
{
writeExceptionToLog(ex.Message.ToString());
}
}

其中byteImage是以字节为单位的图像。所有我需要知道为什么我有错误500或坏的请求,当我张贴照片

在参数模型类中使用IFormFile属性并在参数之前使用[FromForm],这可能会解决swagger和API请求中的问题

public class ProjectDetail
{
public string Name { get; set; }
public IFormFile File { get; set; }
}
[HttpPost("FileUpload")]
public async Task<IActionResult> FileUpload([FromForm] ProjectDetail InpData)
{
return Ok("Success");
}

最新更新