在 dotnet core web 中接收多部分表单数据 API
Receiving Multi-part form data in 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; }
}
在上面class/model“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>
。它不会将整个模型更改为List.So,您还可以检索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 用于检索从发布的表单 data.refer 到 the official document.
的所有值
如果你想使用c,你可以尝试使用它,你可以这样做
public IActionResult CreateInvoice([FromForm]IFormCollection idobj)
并且您需要为 IFormCollection 的每个键获取您想要的数据,因此 public List<IFormCollection> invoice_file { get;set;}
比使用 IFormCollection 更好。
我正在开发 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; }
}
在上面class/model“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>
。它不会将整个模型更改为List.So,您还可以检索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; }
}
结果:
如果你想使用c,你可以尝试使用它,你可以这样做
public IActionResult CreateInvoice([FromForm]IFormCollection idobj)
并且您需要为 IFormCollection 的每个键获取您想要的数据,因此 public List<IFormCollection> invoice_file { get;set;}
比使用 IFormCollection 更好。