ASP.NET 使用 .NET 5.0 的核心 Web 应用程序:从视图传递到控制器时,IFormFile 始终为 null
ASP.NET Core Web Application using .NET 5.0: IFormFile always null when passing from view to controller
我正在尝试让用户上传 pdf 文件。我没有收到任何错误,但控制器中的 IFormFile 'PostedFile' 始终为 null。
创建视图:
<div class="form-group">
<label asp-for="PostedFile" class="control-label"></label>
<div class="col-md-10">
<input type="file" asp-for="PostedFile" />
<span asp-validation-for="PostedFile" class="text-danger"></span>
</div>
控制器,创建方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Name,Phone1,Phone2,District_Division,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log, IFormFile PostedFile)
{
string path = "Case_Log_Docs/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
System.Diagnostics.Debug.WriteLine("Created the folder.");
}
if (PostedFile != null)
{
string fileName = Path.GetFileName(PostedFile.FileName);
System.Diagnostics.Debug.WriteLine(fileName);
PostedFile.CopyTo(new FileStream(path, FileMode.Create));
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
else
{
System.Diagnostics.Debug.WriteLine("Posted file was null.");
}
if (ModelState.IsValid)
{
_context.Add(case_Log);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(case_Log);
}
请注意:我(认为我)不想使用列表,因为我不希望用户一次能够上传超过 1 个文档,因为这些文档有一个相应的数据库条目,带有 1到 1 个关系。
我有几个问题。
1.) 问题是什么?为什么 IFormFile 总是空的?
2.) 为什么人们总是推荐 List 而不是 IFormFile?
将其余变量传递给控制器工作正常:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"> </div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Phone1" class="control-label"></label>
<input asp-for="Phone1" class="form-control" />
<span asp-validation-for="Phone1" class="text-danger"></span>
</div>
但是文件上传 div 仍然在指向 Create 方法的表单中。视图元素有问题吗?如果是这样,我将如何更改它以解决问题?
我尝试按照这个示例进行操作,没有出现任何错误,但也没有结果:https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx
您需要使用 enctype=multipart/form-data
以允许整个文件包含在 data.Like 之后。
<form asp-action="xxx" enctype="multipart/form-data">
//...
<input type="file" name="PostedFile" />
<input type="submit" value="click"/>
</form>
操作:
[HttpPost]
public IActionResult Demo(IFormFile PostedFile)
{
//...
}
结果:
我正在尝试让用户上传 pdf 文件。我没有收到任何错误,但控制器中的 IFormFile 'PostedFile' 始终为 null。
创建视图:
<div class="form-group">
<label asp-for="PostedFile" class="control-label"></label>
<div class="col-md-10">
<input type="file" asp-for="PostedFile" />
<span asp-validation-for="PostedFile" class="text-danger"></span>
</div>
控制器,创建方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Name,Phone1,Phone2,District_Division,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log, IFormFile PostedFile)
{
string path = "Case_Log_Docs/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
System.Diagnostics.Debug.WriteLine("Created the folder.");
}
if (PostedFile != null)
{
string fileName = Path.GetFileName(PostedFile.FileName);
System.Diagnostics.Debug.WriteLine(fileName);
PostedFile.CopyTo(new FileStream(path, FileMode.Create));
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
else
{
System.Diagnostics.Debug.WriteLine("Posted file was null.");
}
if (ModelState.IsValid)
{
_context.Add(case_Log);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(case_Log);
}
请注意:我(认为我)不想使用列表,因为我不希望用户一次能够上传超过 1 个文档,因为这些文档有一个相应的数据库条目,带有 1到 1 个关系。
我有几个问题。
1.) 问题是什么?为什么 IFormFile 总是空的? 2.) 为什么人们总是推荐 List 而不是 IFormFile?
将其余变量传递给控制器工作正常:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"> </div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Phone1" class="control-label"></label>
<input asp-for="Phone1" class="form-control" />
<span asp-validation-for="Phone1" class="text-danger"></span>
</div>
但是文件上传 div 仍然在指向 Create 方法的表单中。视图元素有问题吗?如果是这样,我将如何更改它以解决问题?
我尝试按照这个示例进行操作,没有出现任何错误,但也没有结果:https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx
您需要使用 enctype=multipart/form-data
以允许整个文件包含在 data.Like 之后。
<form asp-action="xxx" enctype="multipart/form-data">
//...
<input type="file" name="PostedFile" />
<input type="submit" value="click"/>
</form>
操作:
[HttpPost]
public IActionResult Demo(IFormFile PostedFile)
{
//...
}
结果: