【发布时间】:2021-03-14 05:18:22
【问题描述】:
我正在开发一个 Web API,它需要在模型中接收多部分表单数据。截至目前,当我从邮递员那里尝试时,它没有收到请求并显示错误请求。 我的模型:
public class InvoiceDetails
{
public int? po_id { get; set; }
public DateTime created_date { get; set; }
public string grn_status { get; set; }
public int utilization_amount { get; set; }
public int currency_id { get; set; }
public string currency_code { get; set; }
public string currency_symbol { get; set; }
[FromForm(Name = "invoice_file")]
public List<IFormCollection> invoice_file { get;set;}
[FromForm(Name = "other_files")]
public List<IFormCollection> other_files { get; set; }
}
在上面的类/模型中,“invoice_file”和“other_files”可以有多个文件上传,所以我把它做成了List。
我的行动方法:
[HttpPost]
[Route("CreateInvoice")]
public IActionResult CreateInvoice([FromForm]InvoiceDetails idobj )
{
//var modelData = Request.Form["invoice_file"];
Response resobj = new Response();
try
{
if (idobj.invoice_file.Count > 0)
{
resobj = _dataContext.AddInvoice(idobj);
if (resobj.flag == true)
{
Upload(idobj);
}
}
else
{
resobj.flag = false;
resobj.message = "please upload atleast one invioce file";
}
}
catch (Exception ex)
{
}
return Ok(resobj);
}
如何制作操作方法或模型,以便用户可以将带有多个文件的模型上传到属性 other_files 和 invoice_file。
【问题讨论】:
-
你的意思是
List<IFormFile>?另外,请出示您的 Postman 请求。 -
@CodeCaster ,是的,但它应该接收多个文件。请检查我的参考帖子人图片。
-
@CodeCaster 在您提供的链接中,他只能发布文件(一个参数)。但在这里我期待的 Objecct 也可能包含文件。如何以这种方式设计我的 API 或操作方法..
-
通过使用
List<IFormFile>并设置适当的请求标头。不要因为存在差异而忽略相关链接,请查看相似之处,在这种情况下是错误消息。
标签: asp.net-web-api asp.net-core-webapi webapi