【问题标题】:IFormFiles always receive a null even if the form has the enctype="multipart/form-data" tag即使表单具有 enctype="multipart/form-data" 标记,IFormFiles 也始终收到 null
【发布时间】:2017-01-23 08:11:19
【问题描述】:

谁能告诉我,当我们在方法中指定 [FromBody] 标签时,asp.net core mvc framework 会做什么?

// POST api/values
[HttpPost]
public IActionResult register ([FromBody]CustomerViewModel custView)
{
    if (ModelState.IsValid)
    {
     repo.create_customer(custView);
     return Ok(custView);
    }
    return Redirect("/");
}

这是我们想要接收的视图模型。

public class CustomerViewModel
{
     [Required(ErrorMessage = "Please enter your name!")]
     public string name { get; set; }

     //[Required(ErrorMessage = "Please upload your resume!")]
     //public IFormFile cv { get; set; }

}

我注意到当我不使用[FromBody]标签时,我仍然可以正确接收对象(具有文件作为属性),而当我添加[FromBody]时,它无法通过http接收文件流,这是为什么我们甚至想使用 [FromBody] 标签来接收 http 帖子?


忘了提,即使使用 multipart-tag 也不起作用

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    尝试将表单上的内容类型设置为“multipart/form-data”。这是我们在上传文件时应该使用的。规范是https://www.ietf.org/rfc/rfc1867.txt

    【讨论】:

    • 您好,感谢您的回复,我是按照文档完成的,所以我有带有 html 的 multipart-tag,但它仍然不起作用。
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2021-03-04
    相关资源
    最近更新 更多