사용자가 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.) 문제가 무엇입니까? IFormFile이 항상 null 인 이유는 무엇입니까? 2.) 사람들이 항상 IFormFile보다 List를 권장하는 것처럼 보이는 이유는 무엇입니까?
나머지 변수를 컨트롤러에 전달하면 정상적으로 작동합니다.
<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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다