IFormFile 在 ASP.NET Core 3.0 中返回 NULL
IFormFile is returning NULL in ASP.NET Core 3.0
我已经阅读过与我类似的帖子,但它们没有帮助。
所以发生的事情是我正在关注 kudvenkat 的asp.net 核心 mvc 教程系列。
我有一个简单的表单,其中一些字段工作得很好,它们可以正常发送数据,但是照片上传字段不起作用。每当我点击提交按钮时,应该在 HomeController 中保存文件名的对象 (model.Photo) 的字段是 null。
它可能对 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
我假设您在以下位置遇到错误:
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
类型的值发送到您的操作。
确保您的表单必须包含 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>
我已经阅读过与我类似的帖子,但它们没有帮助。
所以发生的事情是我正在关注 kudvenkat 的asp.net 核心 mvc 教程系列。
我有一个简单的表单,其中一些字段工作得很好,它们可以正常发送数据,但是照片上传字段不起作用。每当我点击提交按钮时,应该在 HomeController 中保存文件名的对象 (model.Photo) 的字段是 null。
它可能对 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
我假设您在以下位置遇到错误:
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
类型的值发送到您的操作。
确保您的表单必须包含 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>