【问题标题】:Receiving Multi-part form data in dotnet core web API在 dotnet core web API 中接收多部分表单数据
【发布时间】: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&lt;IFormFile&gt;?另外,请出示您的 Postman 请求。
  • @CodeCaster ,是的,但它应该接收多个文件。请检查我的参考帖子人图片。
  • @CodeCaster 在您提供的链接中,他只能发布文件(一个参数)。但在这里我期待的 Objecct 也可能包含文件。如何以这种方式设计我的 API 或操作方法..
  • 通过使用List&lt;IFormFile&gt; 并设置适当的请求标头。不要因为存在差异而忽略相关链接,请查看相似之处,在这种情况下是错误消息。

标签: asp.net-web-api asp.net-core-webapi webapi


【解决方案1】:

正如CodeCaster 所说,添加Content-Type:multipart/form-data; 并将List&lt;IFormCollection&gt; 更改为List&lt;IFormFile&gt;。它不会将整个模型更改为List。因此您还可以使用idobj.xxx 检索模型中存在的其他信息。改变

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 用于从已发布的表单数据中检索所有值。请参阅the official document

如果你想使用c,你可以尝试使用它,你可以这样做

public IActionResult CreateInvoice([FromForm]IFormCollection idobj)

并且你需要获取你想要的数据来foreach IFormCollection的键,所以public List&lt;IFormCollection&gt; invoice_file { get;set;}比使用IFormCollection更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多