IFormFile在ASP.NET Core 3.0中返回NULL

时间:2019-12-13 09:15:06

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

我已经阅读了与我相似的帖子,但它们没有帮助。
所以发生了什么事,我正在关注 kudvenkat的 asp.net核心mvc教程系列。
我有一个简单的表单,其中的某些字段可以很好地工作,并且它们可以正常发送数据,但是用于照片上传的字段不起作用。每当我单击“提交”按钮时,应在HomeController中保存文件名的对象(模型。照片)的字段为。 从asp.net核心的2.1版本更改为3.0版本可能会有一些变化,但我只是在猜测。

Create.cshtml (我只粘贴了该字段的代码并提交按钮,表单方法设置为POST)

<div class="form-group">
     <label asp-for="Photo"></label>
     <div class="custom-file">
        <input asp-for="Photo" class="form-control custom-file-input" formenctype="multipart/form-data"/>
        <label class="custom-file-label">Choose file...</label>
     </div>
</div>
<div>
    <button class="btn btn-secondary" type="submit"><i class="fas fa-plus-square"></i> Create</button>
</div>

HomeController.cs (我只为这个问题粘贴了有趣的代码,如果您需要,我可以显示其余代码)

public IActionResult Create(EmployeeCreateViewModel model)
        {
            if(ModelState.IsValid)
            {
                string uniqueFileName = null;
                if(model.Photo != null)
                {
                    string uploadsFolder = Path.Combine(hostingEnviroment.WebRootPath, "img");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName);
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                Employee newEmployee = new Employee
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Department = model.Department,
                    Job = model.Job,
                    Address = model.Address,
                    Birthday = model.Birthday,
                    PhotoPath = uniqueFileName
                };

                _employeeRepository.Add(newEmployee);

                return RedirectToAction("Details", new { id = newEmployee.Id });
            }

            return View();
        }

EmployeeCreateViewModel.cs

    public class EmployeeCreateViewModel
    {
        [Required]
        [Display(Name = "First name")]
        public string FirstName { get; set; }
        [Required]
        [Display(Name = "Last name")]
        public string LastName { get; set; }
        [Required]
        [RegularExpression(@"^[a-zA-Z0-9_.+-]+@coreuniverse.com",
        ErrorMessage = "Invalid email format. Follow pattern: etc@coreuniverse.com")]
        public string Email { get; set; }
        [Required]
        public Dept? Department { get; set; }
        [Required]
        public string Job { get; set; }
        [Required]
        public string Birthday { get; set; }
        [Required]
        public string Address { get; set; }
        public IFormFile Photo { get; set; }
    }

This picture shows a breakpoint when Create() action is triggered. It proves that the value is NULL

2 个答案:

答案 0 :(得分:1)

我认为您正面临错误:

if(model.Photo != null)
{
      string uploadsFolder = Path.Combine(hostingEnviroment.WebRootPath, "img");
      uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.Photo.FileName);
      string filePath = Path.Combine(uploadsFolder, uniqueFileName);
      model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
}

首先,您可以尝试如下更改您的输入字段

<input asp-for="Photo" type='file' value='@Model.Photo' class="form-control custom-file-input" formenctype="multipart/form-data"/>

添加了type='file'value='@Model.Photo'

现在,您应该将IFormFile值的类型发送给您的操作。

答案 1 :(得分:1)

确保您的表单必须包含enctype="multipart/form-data",如下所示:

<form  method="post" enctype="multipart/form-data">
    <div class="form-group">
       <label asp-for="Photo"></label>
       <div class="custom-file">
           <input asp-for="Photo" class="form-control custom-file-input"/>
           <label class="custom-file-label">Choose file...</label>
       </div>
    </div>
    <input type="submit" value="Upload Image" name="submit">
</form>