asp.net-web-api - 在 dotnet core web API 中接收多部分表单数据
问题描述
我正在开发一个 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”可以有多个文件上传,所以我把它列出来了。
我的行动方法:
[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。
解决方案
如前所述CodeCaster
,添加Content-Type:multipart/form-data;
并更改List<IFormCollection>
为。List<IFormFile>
它不会将整个模型更改为列表。因此您还可以使用 idobj.xxx.Change 检索模型中存在的其他信息
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; }
}
到
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<IFormFile> invoice_file { get; set; }
[FromForm(Name = "other_files")]
public List<IFormFile> other_files { get; set; }
}
结果: IFormCollection 用于从已发布的表单数据中检索所有值。参考官方文档。
如果你想使用c,你可以尝试使用它,你可以这样做
public IActionResult CreateInvoice([FromForm]IFormCollection idobj)
并且你需要获取你想要的数据来获取 IFormCollection 的每个键,所以public List<IFormCollection> invoice_file { get;set;}
比使用 IFormCollection 更好。
推荐阅读
- excel - 我不明白这个公式在 VBA 的列中获取唯一值
- sql-server - 当功能需要版本 14 或 16 或更高版本时,为什么 azure synapse 在版本 12 中创建 SQL Server?
- azure - 如何在 TrainCustomModelAsync 表单识别器上定位本地路径
- java - Java 使用 HTTP GET 请求打开 Web 浏览器到 URI
- webpack - 为什么在我的设置中使用“@babel/preset-env”和“@babel/preset-typescript”需要“@babel/plugin-proposal-optional-chaining”
- r - 使用插入符号预处理训练和测试数据
- java - 需要代码设计反馈:检查文件中的字符是否是允许的字符
- angular - 什么是在绝对空的 Angular 应用程序中触发计时器?
- python - 为什么 gettext (Python) 没有 .mo 文件就不能工作?
- vim - 在 vim 中跨机器共享标记