[英]IFormFile is returning NULL in ASP.NET Core 3.0
我已经阅读过与我类似的帖子,但它们没有帮助。
所以发生的事情是我正在关注kudvenkat 的asp.net core mvc 教程系列。
我有一个简单的表单,其中包含一些可以正常工作的字段,并且它们正在正常发送数据,但是用于照片上传的字段不起作用。 每当我点击提交按钮时,应在 HomeController 中保存文件名的对象 ( model.Photo ) 的字段为null 。 它可能有一些从 2.1 到 3.0 版本的 asp.net core 的更改,但我只是猜测。
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();
}
员工创建视图模型.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; }
}
我假设您正面临以下错误:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.