首页 > 解决方案 > 在 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。

邮递员图片参考邮递员参考图片

标签: asp.net-web-apiasp.net-core-webapiwebapi

解决方案


如前所述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 更好。


推荐阅读